I am trying to create a basic proxy server 4, and I need to parse the package and extract its information, which 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 is my code:
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);
reader.read(cmd, 1, 1);
reader.read(port, 2, 2);
reader.read(ip, 4, 4);
Here is the answer I get from my code: [4, 1, 0, 80, -39, 70, -74, -94]
For some reason, the part of the IP that I get is always wrong, I really don't know why. My second problem would be: is there a simple and clean way to get the last line of userID without creating a messy loop that could hang forever if the byte was \0not found?
Thank.
source
share