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

EndReceive() public method

public EndReceive ( IAsyncResult asyncResult, IPEndPoint &remoteEP ) : byte[]
asyncResult IAsyncResult
remoteEP IPEndPoint
return byte[]
        public byte[] EndReceive(IAsyncResult asyncResult, ref IPEndPoint remoteEP)
        {
            if (_cleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            EndPoint tempRemoteEP;
            if (_family == AddressFamily.InterNetwork)
            {
                tempRemoteEP = IPEndPointStatics.Any;
            }
            else
            {
                tempRemoteEP = IPEndPointStatics.IPv6Any;
            }

            int received = _clientSocket.EndReceiveFrom(asyncResult, ref tempRemoteEP);
            remoteEP = (IPEndPoint)tempRemoteEP;

            // Because we don't return the actual length, we need to ensure the returned buffer
            // has the appropriate length.
            if (received < MaxUDPSize)
            {
                byte[] newBuffer = new byte[received];
                Buffer.BlockCopy(_buffer, 0, newBuffer, 0, received);
                return newBuffer;
            }

            return _buffer;
        }

Usage Example

Example #1
0
        private void OnReceived(IAsyncResult ar)
        {
            if (Client != null)
            {
                IPEndPoint endpoint = new IPEndPoint((EndPoint as IPEndPoint).Address, 0);

                byte[] data;
                try
                {
                    data = Client.EndReceive(ar, ref endpoint);
                }
                catch (SocketException)
                {
                    Dispose();
                    return;
                }
                catch (ObjectDisposedException)
                {
                    Dispose();
                    return;
                }
                LastActivity = DateTime.UtcNow;
                Client.BeginReceive(new AsyncCallback(OnReceived), null);
                Reader.ReceivedData(data);
            }
        }
All Usage Examples Of System.Net.Sockets.UdpClient::EndReceive