示例#1
0
 /// <summary>
 /// This method is invoked when an asynchronous send operation completes.  The method issues another receive
 /// on the socket to read any additional data sent from the client
 /// </summary>
 /// <param name="e"></param>
 private void ProcessSend(SocketAsyncEventArgs e)
 {
     if (e.SocketError == SocketError.Success)
     {
         // done echoing data back to the client
         AsyncUserToken token = (AsyncUserToken)e.UserToken;
         // read the next block of data send from the client
         bool willRaiseEvent = token.Socket.ReceiveAsync(e);
         if (!willRaiseEvent)
         {
             ProcessReceive(e);
         }
     }
     else
     {
         CloseClientSocket(e);
     }
 }
示例#2
0
        /// <summary>
        /// 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.
        /// </summary>
        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 _totalBytesRead, e.BytesTransferred);
                Console.WriteLine("The server has read a total of {0} bytes", _totalBytesRead);

                Int32 remainingBytesToProcess = e.BytesTransferred;

                //remainingBytesToProcess = HandlePrefix(receiveSendEventArgs, receiveSendToken, remainingBytesToProcess);
                byte[] prefix = new Byte[_receivePrefixLength];
                byte[] data   = new byte[_receiveBufferSize];

                Buffer.BlockCopy(e.Buffer, 0, prefix, 0, _receivePrefixLength);
                remainingBytesToProcess = remainingBytesToProcess - _receivePrefixLength;

                int recievedDataLength = remainingBytesToProcess;
                Buffer.BlockCopy(e.Buffer, _receivePrefixLength, data, 0, remainingBytesToProcess);

                byte[] returnData   = GetAnswer(data, remainingBytesToProcess);
                byte[] answerPrefix = BitConverter.GetBytes(returnData.Length);

                ResetBuffer(e);
                Buffer.BlockCopy(answerPrefix, 0, e.Buffer, 0, _receivePrefixLength);
                Buffer.BlockCopy(returnData, 0, e.Buffer, _receivePrefixLength, returnData.Length);

                //echo the data received back to the client
                bool willRaiseEvent = token.Socket.SendAsync(e);
                if (!willRaiseEvent)
                {
                    ProcessSend(e);
                }
            }
            else
            {
                CloseClientSocket(e);
            }
        }
示例#3
0
        private void CloseClientSocket(SocketAsyncEventArgs e)
        {
            AsyncUserToken token = e.UserToken as AsyncUserToken;

            // close the socket associated with the client
            try
            {
                token.Socket.Shutdown(SocketShutdown.Send);
            }
            // throws if client process has already closed
            catch (Exception)
            {
            }
            token.Socket.Close();

            // decrement the counter keeping track of the total number of clients connected to the server
            Interlocked.Decrement(ref _numConnectedSockets);
            _maxNumberAcceptedClients.Release();
            Console.WriteLine("A client has been disconnected from the server. There are {0} clients connected to the server", _numConnectedSockets);

            // Free the SocketAsyncEventArg so they can be reused by another client
            _readWritePool.Push(e);
        }