[C++] Send a packet

12/31/2012 01:31 -X-Ni-ck-X-#1
Hi... iam not very good with C++, but i made simple pixel bot for little mmorpg game which is working... But i want add to this bot "send packet" function, which will send one simple packet from a game client to the server... The packet what i want to send i found with WPE PRO... My question is, is there anybody, who could help me with sending a one packet? Or send me a tutorial or anything that can help me... Thank you so much and sorry for my english.. :)
12/31/2012 02:30 .SkyneT.#2
[Only registered and activated users can see links. Click Here To Register...]
12/31/2012 12:50 .Infinite#3
I once began writing a class for http packets using winsocks... It's probably bad, but it should give you an idea on how to start.

Code:
#pragma comment(lib, "Ws2_32.lib")
#include <Windows.h>
#include <iostream>
#include <string>
#include <sstream>


class packet
{
	private:
		int Socket;
		char* buf;
		int size;
		sockaddr_in service;
	public:
		packet(bool pPost, string pLocation, string pHost);
		bool Post;
		string Location;
		string Host;
		string Send();
		~packet();
};

packet::packet(bool pPost, string pLocation, string pHost)
{
	Post = pPost;
	Location = pLocation;
	Host = pHost;

	WSADATA w;
	WSAStartup(MAKEWORD(2,2), &w);
	Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	service.sin_family = AF_INET;
	service.sin_port = htons(80);
	service.sin_addr.s_addr = inet_addr("127.0.0.1"); // IP
};

string packet::Send()
{
	string Request;
	if (Post == true) // incomplete
	{
		Request += "POST ";
	}
	else
	{
		Request += "GET ";
	}
	Request += Location + " HTTP/1.1\r\n";
	Request += "Host: " + Host + "\r\n";
	Request += "Connection: close\r\n\r\n";

	unsigned int bytesSent = 0;
	connect(Socket, reinterpret_cast<sockaddr*>(&service), sizeof(service));
	do
	{
		bytesSent += send(Socket, Request.c_str() + bytesSent, Request.size() - bytesSent, 0);
    } while(bytesSent < Request.size());

	string Response;
	char ReceivedBytes[256];
	int BytesReceived = 1;
	while(BytesReceived > 0)
	{
		BytesReceived = recv(Socket, ReceivedBytes, sizeof(ReceivedBytes), 0);
		Response.append(ReceivedBytes, BytesReceived);
	}
	return Response;
};

packet::~packet()
{
	closesocket(Socket);
	WSACleanup();
};
12/31/2012 12:51 buFFy!#4
A better way would be reversing the networkclass of the game and then calling the game-own sendpacket function.
12/31/2012 13:15 -X-Ni-ck-X-#5
Thank you all for helping me... I read about it and its something like "DLL Inject" .. i think ill need take a look for this. :)
12/31/2012 22:06 MoepMeep#6
Quote:
Originally Posted by buFFy! View Post
A better way would be reversing the networkclass of the game and then calling the game-own sendpacket function.
No.
01/07/2013 12:09 xINKtn#7
Quote:
Originally Posted by MoepMeep View Post
No.
Why not?
I think it would be interesting to know why you think that this way isn't good.

Regards, xINK
01/07/2013 17:59 tnd0#8
because you would either need to block the game's networking thread from sending or risk a race condition, which is a recipe for guaranteed disaster.
01/07/2013 18:26 »Barney«#9
[Only registered and activated users can see links. Click Here To Register...]

This is in C#, I will come back with the C++ version in 2 mins.

C++
Code:
int SendPacket(Socket ^s, String ^str) {
		try {
			str += L"\n\0";
			array<Byte> ^packet = Encoding::UTF8->GetBytes(str);
			if(s->Connected)
			    s->Send(packet,str->Length,SocketFlags::None);
		}
		catch(SocketException ^e) {
			return e->ErrorCode;
		}
                return 1;
01/07/2013 21:06 xNopex#10
Quote:
Originally Posted by chichi011 View Post

This is in C#, I will come back with the C++ version in 2 mins.

C++
Code:
int SendPacket(Socket ^s, String ^str) {
		try {
			str += L"\n\0";
			array<Byte> ^packet = Encoding::UTF8->GetBytes(str);
			if(s->Connected)
			    s->Send(packet,str->Length,SocketFlags::None);
		}
		catch(SocketException ^e) {
			return e->ErrorCode;
		}
                return 1;
That's not C++
[Only registered and activated users can see links. Click Here To Register...]
01/10/2013 19:03 ernilos#11
Quote:
Originally Posted by xNopex View Post
That's not C++
[Only registered and activated users can see links. Click Here To Register...]
C++ with .net framework
Mm...

void Send(socket sck, std::string msg)
{
send(sck, msg.c_str(), msg.lenght() + 1, 0);
}