Problem with an existing connection was forcibly closed by the remote host

Hi,

I got a very strange problem on WM5. My code works fine on PC2002, PC2003, .NET1.1 (WinXP) but it does not on WM5. The code works fine when I query a web-site that respondes with Keep-Alive tag and it doesn't work when a web-site respondes with Proxy-Connection : Close.

The code is very simple - it accepts connections and for each connection creates two sockets, one is accepted socket and the other soket is to a proxy server. Then the code just passes queries-responses between browser on PDA and the proxy server. There are 2 threads per connection, one thread reads from browser and writes to my proxy server, another thread reads from the proxy and writes to the browser.

I have removed all unnecessary code to make the problem clear:

public Form1()

{

InitializeComponent();

int proxyPort = 8090; //local port

int serverPort = 8083; // proxy port

string serverIP = "123.123.123.123"; /proxy address

IPEndPoint iepProxy = new IPEndPoint(IPAddress.Any, proxyPort);

Socket proxySocket = new Socket(iepProxy.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

proxySocket.Bind(iepProxy);

proxySocket.Listen(50);

IPEndPoint iepServer = new IPEndPoint(IPAddress.Parse(serverIP), serverPort);

while (true)

{

   Socket cSocket = proxySocket.Accept();

   Socket pSocket = new Socket(iepServer.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

   pSocket.Connect(iepServer);

   toProxy pRead = new toProxy(cSocket, pSocket); //read from browser, write to proxy

   toProxy pWrite = new toProxy(pSocket, cSocket); //write to browser, read from proxy

   new Thread(new ThreadStart(pRead.Run)).Start();

   new Thread(new ThreadStart(pWrite.Run)).Start();

}

}

 

public class toProxy

{

Socket readSocket = null;

Socket writeSocket = null;

public toProxy(Socket readSocket, Socket writeSocket)

{

   this.writeSocket = writeSocket;

   this.readSocket = readSocket;

}

public void Run()

{

byte[] buff = new byte[0x1000];

int count = -1;

try

{

   while (count != 0)

   {

      count = readSocket.Receive(buff);

      if (count != 0)

         writeSocket.Send(buff, count, SocketFlags.None);

   }

}

catch{}

finally

{

   try

   {

      writeSocket.Shutdown(SocketShutdown.Both);

   }

   catch { }

   try

   {

      readSocket.Shutdown(SocketShutdown.Both);

   }

   catch { }

// System.Threading.Thread.Sleep(500);

   readSocket.Close();

   writeSocket.Close();

}

}

}

 

now, when a web server respondes with Proxy-Connection: close

HTTP/1.0 200 OK

Date: Fri, 07 Jul 2006 02:06:11 GMT

Server: IBM_HTTP_Server

Cache-Control: max-age=4

Expires: Fri, 07 Jul 2006 02:06:15 GMT

Content-Type: text/html; charset=iso-8859-1

Content-Language: en-US

Proxy-Connection: close

IE/Opera gives an error "connection lost" (on WM5 only). When a web server respondes with Keep-Alive, everything is fine.

The most interesting is that if I uncomment the following line, everything works.

// System.Threading.Thread.Sleep(500);

So what is so special in WM5 compared to other WMs And what is the magic with putting thread into a sleep

I used Ethereal to check ip-stream - everything looks OK between proxy and the code, however I was not able to check ip flow between the code and browser.

TIA,

Alex



Answer this question

Problem with an existing connection was forcibly closed by the remote host

  • Fernando Simonazzi

    It possibly relates to a timing issue on socket shutdowns. Given the specificity of the issue, it is unlikely this would be a NETCF issue. Does the issue reproduce on all WM 5 devices Did you try the WM 5 device emulator On TCP connection shutdown, a FIN packet is sent from the client when the call returns, while the server receives the FIN packet at a later (albeit short) time. You may want to investigate if there is any complications around the timing here that may have caused the issue.

    Cheers,

    Anthony Wong [MSFT]


  • BullRush37

    It possibly relates to a timing issue on socket shutdowns. Given the specificity of the issue, it is unlikely this would be a NETCF issue. Does the issue reproduce on all WM 5 devices Did you try the WM 5 device emulator On TCP connection shutdown, a FIN packet is sent from the client when the call returns, while the server receives the FIN packet at a later (albeit short) time. You may want to investigate if there is any complications around the timing here that may have caused the issue.

    Cheers,

    Anthony Wong [MSFT]


  • Koruyucu

    It possibly relates to a timing issue on socket shutdowns. Given the specificity of the issue, it is unlikely this would be a NETCF issue. Does the issue reproduce on all WM 5 devices Did you try the WM 5 device emulator On TCP connection shutdown, a FIN packet is sent from the client when the call returns, while the server receives the FIN packet at a later (albeit short) time. You may want to investigate if there is any complications around the timing here that may have caused the issue.

    Cheers,

    Anthony Wong [MSFT]


  • Problem with an existing connection was forcibly closed by the remote host