System.Net.Sockets.Socket.EndReceive C# (CSharp) Method

EndReceive() public method

public EndReceive ( IAsyncResult asyncResult ) : int
asyncResult IAsyncResult
return int
        public int EndReceive(IAsyncResult asyncResult)
        {
            SocketError errorCode;
            int bytesTransferred = EndReceive(asyncResult, out errorCode);
            if (errorCode != SocketError.Success)
            {
                throw new SocketException((int)errorCode);
            }
            return bytesTransferred;
        }

Same methods

Socket::EndReceive ( IAsyncResult asyncResult, SocketError &errorCode ) : int

Usage Example

示例#1
0
        /// <summary>
        /// Gets triggered when the socket receives something
        /// </summary>
        /// <param name="ar">async result</param>
        private void OnReceiveCallback(IAsyncResult ar)
        {
            try
            {
                if (_socket == null)
                {
                    return;
                }

                var bytesRead = _socket.EndReceive(ar);

                if (bytesRead <= 0)
                {
                    Close();
                    return;
                }

                /**
                 * I thought the problem was this line because it triggers the command handler without checking if the packet
                 *   is "complete" it only checks if "its not null"
                 */
                //Console.WriteLine("BytesRead: {0} | Packet Length: {1}", bytesRead, BitConverter.ToInt16(new byte[] {_readBuffer[1], _readBuffer[0]}, 0));
                //if (BitConverter.ToInt16(new byte[] {_readBuffer[1], _readBuffer[0]}, 0) + 2 == bytesRead)
                //{
                //    OnReceiveData(_readBuffer);
                //}// what happens if it's incomplete just ignores it and waits for the next packet.. i'm not sure if that should work as expected
                OnReceiveData(_readBuffer);

                //Setup over
                _readBuffer = new byte[BufferSize];
                _socket.BeginReceive(_readBuffer, 0, _readBuffer.Length, 0, OnReceiveCallback, this);
            }
            catch { Close(); }
        }
All Usage Examples Of System.Net.Sockets.Socket::EndReceive