Posts: 8
Threads: 3
Joined: Feb 2012
Thanks: 0
Given 0 thank(s) in 0 post(s)
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.
Posts: 1,450
Threads: 53
Joined: Feb 2011
Thanks: 15
Given 120 thank(s) in 91 post(s)
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?
Posts: 8
Threads: 3
Joined: Feb 2012
Thanks: 0
Given 0 thank(s) in 0 post(s)
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.
Posts: 1,450
Threads: 53
Joined: Feb 2011
Thanks: 15
Given 120 thank(s) in 91 post(s)
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
Posts: 8
Threads: 3
Joined: Feb 2012
Thanks: 0
Given 0 thank(s) in 0 post(s)
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.
Posts: 1,450
Threads: 53
Joined: Feb 2011
Thanks: 15
Given 120 thank(s) in 91 post(s)
02-24-2012, 06:05 AM
(This post was last modified: 02-24-2012, 06:06 AM by FakeTruth.)
(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
Posts: 8
Threads: 3
Joined: Feb 2012
Thanks: 0
Given 0 thank(s) in 0 post(s)
Oh Man, really thank you, You solved my problem. Really really thanx.
Posts: 8
Threads: 3
Joined: Feb 2012
Thanks: 0
Given 0 thank(s) in 0 post(s)
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.
|