0

referring to my previous question: Unable to do server reconnection after disconnected in C# winform.

Below is my coding for my client side to do connection with server:

        private void timer1_Tick(object sender, EventArgs e)
    {
        DataProcess();
    }

    public Socket _serverSocket = null;
    private byte[] _recieveBuffer = new byte[128];
    void DataProcess()
    {
        //TimeSpan ts;
        try
        {
            //Thread.Sleep(100);
            if (_serverSocket == null || sockState == MySocketState.Disconnected)
            {
                Console.WriteLine("Trying to connect...");
                //ts = DateTime.Now.Subtract(_dLastActivity);

                //if (ts.TotalSeconds < 0 || ts.TotalSeconds > 10)
                //{
                //StartConnect();
                SetupServer();
                //}
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

    private void SetupServer()
    {
        try
        {
            _serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _serverSocket.Connect(_sIP, Int32.Parse(_sPort));
        }
        catch (SocketException ex)
        {
            Console.WriteLine(ex.Message);
        }

        if (_serverSocket.Connected)
        {
            sockState = MySocketState.Connected;
            browser.ExecuteScriptAsync("svrConnect();");
            Console.WriteLine("Server connected...");
            _serverSocket.BeginReceive(_recieveBuffer, 0, _recieveBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
        }
    }

I do a timer_tick to constantly trying to connect to server. But then a situation occurred: I try connect successfully the server first, then after that I unplug the LAN cable to try to simulate a disconnection. It can't recognize that it is disconnected. It is still in connected state. There's no error message what so ever. Then I plug in the LAN cable back, it can't receive data from server anymore. I think maybe it need to do reconnection again.

How do we let the client know that it has disconnected from server?

Community
  • 1
  • 1
Coolguy
  • 2,043
  • 9
  • 40
  • 74
  • Check this solution: http://stackoverflow.com/questions/2661764/how-to-check-if-a-socket-is-connected-disconnected-in-c – jimmy May 31 '16 at 05:26
  • Yes. My _serverSocket.Connected return true even though it has been disconnected. Would like to know what is the best way to solve this. Is it by constantly pinging the server? – Coolguy May 31 '16 at 06:31
  • Unfortunately I do not think there are any events/triggers connected to this so polling is your best option. See also http://stackoverflow.com/questions/722240/instantly-detect-client-disconnection-from-server-socket – jimmy May 31 '16 at 06:34
  • You mean using by this : return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0); when it return true means it is connected and if return false it is disconnected? – Coolguy May 31 '16 at 06:38
  • I've tested it using return !(socket.Poll(1, SelectMode.SelectRead) && socket.Available == 0); No matter it is connected or disconnected, it stil return true. How? I think this method cannot detect an unplugged cable. – Coolguy May 31 '16 at 06:45

0 Answers0