Cuberite Forum
C++ Minecraft Packet - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Off Topic (https://forum.cuberite.org/forum-9.html)
+--- Forum: Off Topic Discussion (https://forum.cuberite.org/forum-10.html)
+--- Thread: C++ Minecraft Packet (/thread-357.html)



C++ Minecraft Packet - Chlorek - 02-24-2012

Hi, I try to send packets from my own minecraft client, I can send packets but Minecraft server requires that one packet is a few variable types, eg. handshake is ID = 1 (int), username (string) etc. .
So I use winsock and send() function to send packet to server. If I send packet #0 it understands my packet, but when I need to send few parameters to server in one packet I just don't know how to. I tried to send structures, (char/int/BYTE)arrays, but without good results.


RE: C++ Minecraft Packet - FakeTruth - 02-24-2012

When sending data over the network, everything is serialized, that means that structures and what not are converted into a single stream of data.
When you want to send the packet data, you simply send it after you sent the packet id.

I'm not sure how to explain this.. I guess you should maybe look at how MCServer does it?


RE: C++ Minecraft Packet - Chlorek - 02-24-2012

So, if I understand you right I have to do something like this:
1. send packet ID 2
2. send packet char type with username
If tried this method but when I send second one packet it returns error with wrong ID packet.


RE: C++ Minecraft Packet - FakeTruth - 02-24-2012

That probably means you sent the first packet incorrectly.

If you want to send a username (username is a string) you also need to send the length of the string along.
In the minecraft protocol a string is represented by first sending a short (2 bytes) containing the amount of characters in the string, and then the characters


RE: C++ Minecraft Packet - Chlorek - 02-24-2012

And this is my problem, how to send this lenght along a string? I think (but I am not sure), I can not send short and string at same time.
I am going to do like this:
send(sock, (char*)HANDSHAKE, 2, 0); /*HANDSHAKE = 0x02*/
send(sock, (char*)sLenght, 2, 0); /*sLenght is a short with string lenght*/
send(sock, "Chlorek", 7, 0");

I see server after recv() changes packets to integer, and it dectects chlorek (first character of it) as packet 99. What am I doing wrong? I am begginer sockets, so I really thanx you for trying to help me.



RE: C++ Minecraft Packet - FakeTruth - 02-24-2012

(02-24-2012, 05:31 AM)Chlorek Wrote: And this is my problem, how to send this lenght along a string? I think (but I am not sure), I can not send short and string at same time.
I am going to do like this:
send(sock, (char*)HANDSHAKE, 2, 0); /*HANDSHAKE = 0x02*/
send(sock, (char*)sLenght, 2, 0); /*sLenght is a short with string lenght*/
send(sock, "Chlorek", 7, 0");

I see server after recv() changes packets to integer, and it dectects chlorek (first character of it) as packet 99. What am I doing wrong? I am begginer sockets, so I really thanx you for trying to help me.

I fixed it for you
Code:
char PacketID = HANDSHAKE;
send(sock, &PacketID , 1, 0); /*HANDSHAKE = 0x02*/
send(sock, (char*)&sLenght, 2, 0); /*sLenght is a short with string lenght*/
send(sock, "Chlorek", 7, 0");

Also, look here if you haven't already
http://mc.kev009.com/Protocol


RE: C++ Minecraft Packet - Chlorek - 02-24-2012

Oh Man, really thank you, You solved my problem. Really really thanx.


RE: C++ Minecraft Packet - Chlorek - 02-25-2012

I have one question more, so when I send string data packet (with username) server receives for every character from username as 256 bytes. I understand I need to encode this string to UTF-32 to solve this problem, but I heard it should be also send as byte array but it nothing changes. So if anyone can give me a hand with encoding I would be really happy.