0

I'm working on a http proxy application, everything is working fine (client can connect to server and get contents), the problem is neither of HTTP Server or browser close the TCP connection.. I'm not sure if I'm doing it right, here is the code:

while (tcp_link.Connected && _tcp.Connected && !ioError)
{
  try
  {
      Thread.Sleep(100);
      if (streamLink.DataAvailable)
      {
          byte[] l_buffer = new byte[10000];
          int l_read = streamLink.Read(l_buffer, 0, l_buffer.Length);
          byte[] l_data = new byte[l_read];
          Array.Copy(l_buffer, l_data, l_data.Length);

          _stream.Write(l_data, 0, l_data.Length);
      }

      if (_stream.DataAvailable)
      {
          byte[] c_buffer = new byte[10500];
          int c_read = _stream.Read(c_buffer, 0, c_buffer.Length);
          byte[] c_data = new byte[c_read];
          Array.Copy(c_buffer, c_data, c_data.Length);

          streamLink.Write(c_data, 0, c_data.Length);
      }
  }
  catch
  {
      ioError = true;
  }
}

I have same code both sides (proxy client, and proxy server)

NOTE: browser will connect to proxy client (which is on the same computer), and proxy client will connect to proxy server, and obviously proxy server will connect to http server, the reason is i wan't to encode data before sending it out

Shadow Walker
  • 325
  • 1
  • 4
  • 11
  • You can consider initializing the byte[] with the socket.Available. This code does not give an idea of what you are trying to acvhive – ray Oct 07 '12 at 10:45

2 Answers2

1

How long have you observed the connection open for?

It's very likely that the client uses HTTP 1.1 where Persistent Connections are on by default.

If you're writing a proxy then you should consider: http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
  • i used network monitoring utilities, when browser is connecting directly to http server, connection get closed right after data is received, so it means browser or http server will close the connection, but when browser connects to proxy client i cannot detect any close request neither from http server on proxy server side – Shadow Walker Oct 07 '12 at 10:55
1

Okay i found the problem, who ever is out there having problem with closing socket connection..

Actually, Socket.Available isn't working as i expected, to see if a socket is really connected you should check both Available and Poll properties, below function should resolve your problem: thanks to: zendar

bool SocketConnected(Socket s)
{
      bool part1 = s.Poll(1000, SelectMode.SelectRead);
      bool part2 = (s.Available == 0);
      if (part1 & part2)
            return false;
      else
            return true;
}

i hope this solve your problem too ;)

Community
  • 1
  • 1
Shadow Walker
  • 325
  • 1
  • 4
  • 11