C++ Minecraft Packet
#1
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.
Reply
Thanks given by:
#2
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?
Reply
Thanks given by:
#3
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.
Reply
Thanks given by:
#4
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
Reply
Thanks given by:
#5
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.
Reply
Thanks given by:
#6
(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
Reply
Thanks given by:
#7
Oh Man, really thank you, You solved my problem. Really really thanx.
Reply
Thanks given by:
#8
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.
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)