Note that there are some explanatory texts on larger screens.

plurals
  1. POC# sockets fail to reconnect
    text
    copied!<p>I have problem which I was trying to solve for some time now, still with no success. We have simple server client app, which works by synchronously exchanging messages.</p> <p>Here is related code.</p> <p>Creating sockets:</p> <pre><code>Socket sender; IPAddress ipAddress = IPAddress.Parse(remoteHost); IPEndPoint remoteEP = new IPEndPoint(ipAddress, remotePort); sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sender.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); sender.ReceiveTimeout = 5000; sender.Connect(remoteEP); </code></pre> <p>Receiving data:</p> <pre><code>try { if (!sender.Connected) Reconnect(); int bytesRec = sender.Receive(readBuffer); if (bytesRec == 0) { //warning 0 bytes received } return Encoding.ASCII.GetString(readBuffer, 0, bytesRec); } catch (SocketException se) { //print se.ErrorCode throw; } </code></pre> <p>Sending data:</p> <pre><code>try { if (!sender.Connected) Reconnect(); byte[] msg = Encoding.ASCII.GetBytes(sendMessage + "\0"); int bytesSent = sender.Send(msg); return true; } catch (SocketException se) { //print se.ErrorCode throw; } </code></pre> <p>Reconnect code:</p> <pre><code>if (sender != null) { if (sender.Connected) { sender.Shutdown(SocketShutdown.Both); sender.Disconnect(true); } sender.Close(); IPAddress ipAddress = IPAddress.Parse(RemoteHost); IPEndPoint remoteEP = new IPEndPoint(ipAddress, RemotePort); sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp); sender.ReceiveTimeout = 5000; sender.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); sender.Connect(remoteEP); } </code></pre> <p>Here is what is happening:</p> <p>While receiving the data I get 10060 error code, which means it timed out. So I try to reconnect. Then it keeps throwing the same error code for some time with the following exception: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond n.n.n.n:x</p> <p>Then eventually it manages to "reconnect" but receives 0 bytes. So I try to send some data. It starts failing with WSAECONNABORTED 10053 error. I again try to reconnect. It continues with 10053 error and receiving 0 bytes. It never recovers. </p> <p>So I close the app, start it again, but it cannot connect, server logs say that there are to many opened connections. Problem is that server doesn't see that the client has disconnected... </p> <p>I am a bit lost here. Please help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload