title: How to parse TCP/IP packets from socket in Java?
body: <p>I have InputStream from socket <code>InputStream sin = socket.getInputStream();</code>. I want to parse TCP/IP packets from this socket to get destination host and port.
  How I can to do it?
  Any advice?</p>
  
  <h2>UPD</h2>
  
  <p>When printing out everything of the socket using:</p>
  
  <pre><code>bytes = sin.readAllBytes();
  System.out.println("-&gt; " + sin);
  System.out.println("-&gt; -&gt; " + sin.read());
  for (int i = 0; i &lt; bytes.length; i++) {
      System.out.println("++ -&gt; " + bytes[i]);
  }
  </code></pre>
  
  <p>I have something like that:</p>
  
  <pre><code>-&gt; java.net.SocketInputStream@36d64342
  -&gt; -&gt; -1
  ++ -&gt; 71
  ++ -&gt; 69
  ++ -&gt; 84
  ++ -&gt; 32
  ++ -&gt; 104
  ++ -&gt; 116
  ++ -&gt; 116
  ++ -&gt; 112
  ++ -&gt; 58
  ++ -&gt; 47
  ++ -&gt; 47
  ++ -&gt; 50
  ++ -&gt; 105
  ++ -&gt; 112
  ++ -&gt; 46
  ++ -&gt; 114
  ++ -&gt; 117
  ++ -&gt; 47
  ++ -&gt; 102
  ++ -&gt; 97
  ++ -&gt; 118
  ++ -&gt; 105
  ++ -&gt; 99
  ++ -&gt; 111
  ++ -&gt; 110
  ++ -&gt; 46
  ++ -&gt; 105
  ++ -&gt; 99
  ++ -&gt; 111
  ++ -&gt; 32
  ++ -&gt; 72
  ++ -&gt; 84
  ++ -&gt; 84
  ++ -&gt; 80
  ++ -&gt; 47
  ++ -&gt; 49
  ++ -&gt; 46
  ++ -&gt; 49
  ++ -&gt; 13
  ++ -&gt; 10
  ++ -&gt; 72
  ++ -&gt; 111
  ++ -&gt; 115
  ++ -&gt; 116
  ++ -&gt; 58
  ++ -&gt; 32
  ++ -&gt; 50
  </code></pre>
  
  <p>It's a piece of a byte array that i print.</p>
  
  <h2>UPD2</h2>
  
  <p>When i convert bytes to char i have this one:</p>
  
  <pre><code>GET http://2ip.ru/ HTTP/1.1
  Host: 2ip.ru
  Proxy-Connection: keep-alive
  Accept
  text/html,application/xhtml+xml,application/
  xml;q=0.9,image/webp,*/*;q=0.8
  Upgrade-Insecure-Requests: 1
  User-Agent: Mozilla/5.0 (Linux; Android 5.1; M3s Build/LMY47I; wv)
  AppleWebKit/537.36 (KHTML, like Gecko)
  Version/4.0 Chrome/44.0.2403.146     Mobile Safari/537.36
  Accept-Encoding: gzip, deflate
  Accept-Language: ru-RU,en-US;q=0.8
  Cookie: PHPSESSID=enph0vckkk9t1hqaglch3b6693;
  IP%5B1511541539%5D=96.94.32.50;
  __gads=ID=88de3c23be0f84e7:T=1511541553:S
  =ALNI_MZbnsWDJcRSBUodYKfLeCg9pjw
  YQw; _ym_uid=1511541569449405305; 
  IP%5B1511886512%5D=217.77.220.27;
  __utma=260173902.2002453512.1511541552.1511886514.
  1511953525.5;
  __utmc=260173902; __utmz=260173902.1511541565.1.1.utmcsr
  =(direct)|utmccn=    (direct)|utmcmd=(none)
  X-Requested-With: com.vestasoft.vadim.proxynator
  </code></pre>