3

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.

Rezoan
  • 1,745
  • 22
  • 51

4 Answers4

1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

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.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • When one side gives a 'connection reset by peer' error, it means that on the wire an ***RST*** packet has been sent by the peer. The local can also give 'connection reset' for *local* reasons, such as the failure to get any ACKs for packets transmitted, in which case *nothing* appears on the wire except those transmissions. In neither case does FIN have anything to do with it. -1 – user207421 Sep 05 '13 at 06:11
1

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.

Mark Douglas
  • 345
  • 2
  • 11
0

how to handle such type of problem i have mentioned?

Close the socket and initiate a new connection.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Ya when i restart the client it creates a new connection and works fine. but i want to know the reason behind this. also i don't want to restart the client app. and there is no why to re-initiate the connection after it successfully disconnected. please see the edit – Rezoan Jul 29 '13 at 12:05
  • @Rezoan, CodeCaster is correct. You cannot expect a connection to remain connected these days. Your application needs to be able to reconnect on loss of connection, and also to cope with periods of network outage from seconds to days. You cannot just assume the connection will be there - you need to plan for what to do WHEN, not IF, you lose the connection. – Ben Jul 29 '13 at 12:11
  • @Ben I'm not saying that the CodeCaster is wrong. i know that i have to re initiate the connection. But at this point when server gets a SocketError.ConnectionReset from any peer, we cannot send further command to force him to reset the socket connection if peer doesn't reset it by himself. i ask the solution of how to reset this in such situation? you can see the edit – Rezoan Jul 29 '13 at 12:24
  • 1
    Please explain what you are trying to do. You can't send or receive anything over a socket after a connection reset. You can't enforce a disconnected client to reconnect from the server, your client should detect and initiate that itself. A disconnected connection goes faulted as soon as you try to send anything over it. – CodeCaster Jul 29 '13 at 12:25
  • @CodeCaster exactly client should detect and initiate that itself but sometimes client is not disconnecting in such situation. i have edited the question again you can see. – Rezoan Jul 29 '13 at 12:33
  • 2
    @Rezoan, Why does it matter whether the client knows the connection is reset? You need to answer that question first. The answer will guide you to the appropriate course of action - which is probably some sort of "heartbeat" message in your application. See this answer for more info: http://stackoverflow.com/questions/5338352/asyncsockets-and-silent-disconnections/5338459#5338459 – Ben Jul 29 '13 at 12:50