none
Socket Receive Callback Question

    Question

  • We provide the following callback to a call to BeginReceive in a Socket object...

            private void ReceiveCallback(IAsyncResult result)
            {
                Socket This = (Socket)result.AsyncState;
                if (This.Connected)
                {
                    SocketError err;
    
                    int BytesRead = EndReceive(result, out err);

    When the client sends data, this callback fires, the call to EndReceive returns the number of bytes, an err of "success", and we process the data.

    We notice that if the client drops the connection, this callback fires, still shows connected, but the call to EndReceive returns 0.  err is still success.  If we again call BeginReceive, it continues to fire with a 0 and success.

    Question:  How are we supposed to detect the client closing the connection?  Is the fact that EndReceive returns a 0 a good indication that the client has closed and we should handle it as such?

    Thanks! 

    Curt


    Friday, February 22, 2013 12:11 PM

Answers

  • The best way is for the client to send an end command before closing the connection so the server know that the close connection going to occur.  If the server doesn't get the close command then an error really has occured.   Below is a diagram of how a connection should work.  The server is a dumb slave that simply processes command that the client sends.

    Client                                Server

    Send Command 1 ---------> Process Command 1

    Receive Response 1 <-----   Send Response 1

    Send Command  2 ---------> Process Command 2

    Receive Response 2 <-----   Send Response 2

    Send End Command  ---------> Process End Command

    Receive End Response <-----   Send End Response

    Close Connection ------------->  Get EndReceive Event


    jdweng

    Friday, February 22, 2013 1:26 PM

All replies

  • The best way is for the client to send an end command before closing the connection so the server know that the close connection going to occur.  If the server doesn't get the close command then an error really has occured.   Below is a diagram of how a connection should work.  The server is a dumb slave that simply processes command that the client sends.

    Client                                Server

    Send Command 1 ---------> Process Command 1

    Receive Response 1 <-----   Send Response 1

    Send Command  2 ---------> Process Command 2

    Receive Response 2 <-----   Send Response 2

    Send End Command  ---------> Process End Command

    Receive End Response <-----   Send End Response

    Close Connection ------------->  Get EndReceive Event


    jdweng

    Friday, February 22, 2013 1:26 PM
  • Understood.  But in your example, what exactly is the "EndReceive" event?  I don't see that dot-net's Socket class has that.  In days of old when we used winsock, we had a notification of when the client closed the connection.  MFC even exposed the event in its api.  So what's the equivalent in dot-net's Socket class?

    Friday, February 22, 2013 2:38 PM
  • All the socket events are on the webpage below

    http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx


    jdweng

    Friday, February 22, 2013 7:58 PM