0

I have two applications, one connects to another via TCP Socket. I was having an issue and after a long troubleshooting I begun to think the root cause is due to the disconnection of the Socket, aka the Socket.state changes to Disconnected.

The reasons I came to above conclusion are just purely from reading the codes and analyze them. I need to prove that is the case and therefore my question is have you ever came accross this type of issue that the socket actually keep getting disconnected even after trying to connect to them?

Below is my Connect code, I have a loop that constantly check for the socket's state itself, if I detect the state is "Disconnected" I call this Connect() function again. Upon each and every time I call Connect() I noticed my socket state is back to Connected again.

So my questions are:

 1.  Have you seen this behavior yourself before?
 2.  Do you see any problem in me calling multiple Connect() again and again?
 3.  Is there a way to simulate this type of socket disconnections?  I tried but I can't set the Socket.Connected flag.


public override void Connect()
    {
        try
        {
            sState = Defs.STATE_CONNECTING;

            // send message to UI
            string sMsg = "<Msg SocketStatus=\"" + sState + "\" />";
            HandleMessage(sMsg);

            // Create the socket object
            sSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            string sIP = "";

            // Define the Server address and port
            if (Validate.IsIPAddress(sServer.ToString()))
            {
                sIP = sServer.ToString();
            }
            else
            {
                IPHostEntry iphost = Dns.GetHostEntry(sServer.ToString());
                sIP = iphost.AddressList[0].ToString();
            }

            IPEndPoint epServer = new IPEndPoint(IPAddress.Parse(sIP), 1234);

            // Connect to Server non-Blocking method
            sSock.Blocking = false;
            AsyncCallback onconnect = new AsyncCallback(OnConnect);
            sSock.BeginConnect(epServer, onconnect, sSock);
        }
        catch (Exception ex)
        {
            LogException(new Object[] { ex });
        }
    }
Fylix
  • 2,551
  • 6
  • 45
  • 72
  • Setting sockets to non-blocking mode is not required if you use the asynchronous (Beginxxx) methods. I don't know if it causes strange behavior if you do, but there's nothing else I can say given the code you shared. – C.Evenhuis Jul 03 '12 at 20:41
  • How do you check if the socket's connected? Note that just checking the Connected property is not a reliable way of determining the socket's state (for instance it won't detect a half-open socket). http://stackoverflow.com/questions/515458/how-can-i-check-whether-a-tcp-socket-is-disconnected-in-c – Kongress Jul 03 '12 at 20:45
  • Kongress, assumed that it is a half-open socket, and that I checked my Connected property, from what I know it reads the state of the last established messsage... doesn't this mean I should get Connected = false instead of true (which is what I'm getting). I check the socket's connected by puting an if(socket.connected) within the OnConnect(IAsyncResult ar) function, this is the callback function you would have seen in the main Connect function – Fylix Jul 03 '12 at 21:00

0 Answers0