Reputation: 131
Im trying to build a basic proxy 4 server, and I have to parse a packet and extract its informations that looks like this:
1 byte (version)
1 byte (command)
2 byte (port)
4 byte (ip)
X byte (userID, builds a string by looping until '\0' is found)
Here's my code so far:
InputStream reader = socket.getInputStream();
byte[] ver = new byte[1];
byte[] cmd = new byte[1];
byte[] port = new byte[2];
byte[] ip = new byte[4];
reader.read(ver, 0, 1); //should be: 4
reader.read(cmd, 1, 1); //should be: 1
reader.read(port, 2, 2); //should be: 00, 80
reader.read(ip, 4, 4); //should be: 217, 70, 182, 162
Here's the response I get from my code: [4, 1, 0, 80, -39, 70, -74, -94]
For some reason the IP part I get is always wrong, I really don't know why. My second issue would be: Is there an easy and clean way to get the last userID string part, without having to build a messy loop that could end up hanging for ever, if the \0
byte was not found?
Upvotes: 4
Views: 301
Reputation: 311008
Throw it all away and use the methods of DataInputStream
. They will give you ints, shorts, longs, and fully-read byte arrays, and take care of network byte ordering for you as well.
Upvotes: 4
Reputation: 19168
The first issue which you received is all because of byte-overflow and hence,turning to negative numbers as byte ranges from -128 to 127 in Java.
Check this question which I asked on this forum to know about the magic(issues) of byte[]...
Seriously,if this is your approach for last-field,(ip)---I am sure you're not going to get correct answer using direct reforms on byte. The possible solution seems to use other approach like storing in temporary int[], like
int[] ip = new int[4];
byte[] temp = new byte[4];
reader.read(temp, 4, 4); //should be: 217, 70, 182, 162
for(int i=0;i<temp.length;i++)
{
if(temp[i]<0)
ip[i]=(int)(256+temp[i]); // careful here
else
ip[i]=(int)temp[i];
}
And,for the second issue,I think that better solution is get length of String-part using String.length()
.
int len = userID.length(); // assume user-id would be String type
userid = new byte[len]; // notice the difference between userid and userID
reader.read(userid,0,len);
// I am confused as to how are you reading input from user,
// if you clarify further,I'll update my answer...
Upvotes: 2