AsyncSocketSample.Server.ProcessReceive C# (CSharp) Method

ProcessReceive() private method

This method is invoked when an asycnhronous receive operation completes. If the remote host closed the connection, then the socket is closed. If data was received then the data is echoed back to the client.
private ProcessReceive ( SocketAsyncEventArgs e ) : void
e System.Net.Sockets.SocketAsyncEventArgs
return void
        private void ProcessReceive(SocketAsyncEventArgs e)
        {
            // check if the remote host closed the connection
            AsyncUserToken token = (AsyncUserToken)e.UserToken;
            if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
            {
                //increment the count of the total bytes receive by the server
                Interlocked.Add(ref m_totalBytesRead, e.BytesTransferred);
                Console.WriteLine("The server has read a total of {0} bytes", m_totalBytesRead);

                //echo the data received back to the client
                bool willRaiseEvent = token.Socket.SendAsync(e);
                if (!willRaiseEvent)
                {
                    ProcessSend(e);
                }

            }
            else
            {
                CloseClientSocket(e);
            }
        }