views:

196

answers:

0

Hi,

I have Server that needs to echo back data from client continously with a delay of 1 second.

Following is the code.

private void ProcessSend(SocketAsyncEventArgs e){

if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
{
        System.Threading.Thread.Sleep(1000);

        // Done echoing data back to the client.
        Socket s = e.UserToken as Socket;

        Byte[] sendBuffer = Encoding.ASCII.GetBytes(strLastReceivedText);

        // Set the buffer to send back to the client.
        e.SetBuffer(sendBuffer, 0, sendBuffer.Length);

        s.SendAsync(e);
}
else
{
    this.CloseClientSocket(e);
}

}

private void OnIOCompleted(object sender, SocketAsyncEventArgs e){
// Determine which type of operation just completed and call the associated handler.
switch (e.LastOperation)
{
    case SocketAsyncOperation.Receive:
        this.ProcessReceive(e);
        break;
    case SocketAsyncOperation.Send:
        this.ProcessSend(e);
        break;
    case SocketAsyncOperation.Disconnect:
        this.ProcessDisconnect(e);
        break;
    default:
        throw new ArgumentException("The last operation completed on the socket was not a receive or send");
}

}

I am facing following issue - I am not able to detect client socket disconnect (on Server), if the client crashes.

Following is the flow,

  1. Client connects to server and sends a character '*'
  2. Server starts echoing back the character '*' with a delay of 1 second
  3. After 10 seconds, the client crashes or network goes down
  4. s.SendAsync(e); succeeds and triggers IOCompletion that calls ProcessSend(SocketAsyncEventArgs e)

  5. In ProcessSend function e.BytesTransferred has value > 0 e.SocketError is SocketError.Success

  6. Steps 4 and 5 continue without any error.

Wondering where the data is going, as the the client has already crashed.

Regards -Shakir Ahmed