title: How to parse a proxy socks packet in java?
body: <p>Im trying to build a basic proxy 4 server, and I have to parse a packet and extract its informations that looks like this:</p>
  <pre><code>1 byte (version)
  1 byte (command)
  2 byte (port)
  4 byte (ip)
  X byte (userID, builds a string by looping until '\0' is found)
  </code></pre>
  <p>Here's my code so far:</p>
  <pre><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);  //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
  </code></pre>
  <p>Here's the response I get from my code:  <code>[4, 1, 0, 80, -39, 70, -74, -94]</code></p>
  <p>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 <code>\0</code> byte was not found?</p>