Rezoan
Rezoan

Reputation: 1797

What are the possible reasons of SocketError.ConnectionReset in TCP Socket

I have a TCP socket based client server system. Everything works fine but when network is disconnected form client end and reconnect it again i get automatically SocketError.ConnectionReset send form client and regarding this command the socket is closed in the server side. this is also fine.

but when i look in to the client side it shows the socket is still connected with server. (regarding socket is still connected with server [It does not happen every time], sometime it shows disconnected and some times shows connected)

Does it make sense that "server get a SocketError.ConnectionReset from client end but client is still connected"?

So i want to know what is the possible reasons of SocketError.ConnectionReset and how to handle such type of problem i have mentioned?

Again i say, Everything is working fine in normal environment (e.g if i exit the client it is disconnected the socket same for the server)

Thanks in advance.

EDIT:

Here is the code in the client side. actually it's a timer that tick every 3 second through programs lifetime and check if Socket is connected or not if its disconnected then it tries to reconnect again through a new socket instance

private void timerSocket_Tick(object sender, EventArgs e)
        {
            try
            {
                if (sck == null || !sck.Connected)
                {
                    ConnectToServer();
                }
            }
            catch (Exception ex)
            {
                RPLog.WriteDebugLog("Exception occcured at: "+ System.Reflection.MethodBase.GetCurrentMethod().ToString()+"Message: "+ex.Message);
            }
        }

In normal situation (without network disconnect/reconnect) if TCP server get a SocketError.ConnectionReset form any client, in the client side i see clients socket is disconnected and it tries to reconnect it again through the code shown. but when situation happen explained earlier, server gets a SocketError.ConnectionReset but client shows it still connected. though the TCP server shows the reset command is send form the exact client side.

Upvotes: 4

Views: 11826

Answers (4)

Mark Douglas
Mark Douglas

Reputation: 375

The problem with using Socket.Connected as you are is that it only gives you the connected state as at the last Send or Receive operation. i.e. It will not tell you that the socket has disconnected unless you first try to send some data to it or receive data from it.

From MSDN description of the Socket.Connected property:

Gets a value that indicates whether a Socket is connected to a remote host as of the last Send or Receive operation.

So in your example, if the socket was functioning correctly when you last sent or received any data from it, the timerSocket_Tick() method would never call ConnectToServer(), even if the socket was now not connected.

Upvotes: 2

Lex Li
Lex Li

Reputation: 63244

When discussing a TCP/IP issue like this, you must mention the network details between the client and the server.

When one side says the connection is reset, it simply means that on the wire a RST packet appears. But to know who sends the RST packet and why, you must utilize network packet captures (by using Wireshark and any other similar tools),

https://en.wikipedia.org/wiki/Transmission_Control_Protocol

You won't easily find out the cause at .NET Framework level.

Upvotes: 1

user207421
user207421

Reputation: 310980

There are several causes but the most common is that you have written to a connection that has already been closed but he other end. In other words, an application protocol error. When it happens you have no choice but to close the socket, it is dead. However you can fix the underlying cause.

Upvotes: 1

CodeCaster
CodeCaster

Reputation: 151674

how to handle such type of problem i have mentioned?

Close the socket and initiate a new connection.

Upvotes: 0

Related Questions