title: C# Getting packet data
body: <p>I've been trying to write a script that will sniff HTTP headers. So far I've got the socket bound to port 80 and packets seem to be received, but I can't get them into string form. All that outputs is "E" continuously. I changed the bytes into hex earlier and there seems to be some data coming in, but the current code is unable to change the bytes into a string. Is there some other way of decoding the bytes that will give a proper string?</p>
  
  <pre><code>byte[] input = BitConverter.GetBytes(1);
  byte[] buffer = new byte[4096];
  Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
  s.Bind(new IPEndPoint(IPAddress.Parse(strIP), 80));
  s.IOControl(IOControlCode.ReceiveAll, input, null);
  int bytes;
  bytes = s.Receive(buffer);
  while (bytes &gt; 0)
  {
      log(System.Text.Encoding.ASCII.GetString(buffer, 0, bytes));
      bytes = s.Receive(buffer);
  }
  </code></pre>