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,
- Client connects to server and sends a character '*'
- Server starts echoing back the character '*' with a delay of 1 second
- After 10 seconds, the client crashes or network goes down
s.SendAsync(e); succeeds and triggers IOCompletion that calls ProcessSend(SocketAsyncEventArgs e)
In ProcessSend function e.BytesTransferred has value > 0 e.SocketError is SocketError.Success
Steps 4 and 5 continue without any error.
Wondering where the data is going, as the the client has already crashed.
Regards -Shakir Ahmed