Export (0) Print
Expand All

UdpClient.Receive Method

Returns a UDP datagram that was sent by a remote host.

[Visual Basic]
Public Function Receive( _
   ByRef remoteEP As IPEndPoint _
) As Byte()
[C#]
public byte[] Receive(
   ref IPEndPoint remoteEP
);
[C++]
public: unsigned char Receive(
   IPEndPoint** remoteEP
)  __gc[];
[JScript]
public function Receive(
   remoteEP : IPEndPoint
) : Byte[];

Parameters

remoteEP
An IPEndPoint representing the remote host from which the data was sent.

Return Value

An array of type Byte that contains datagram data.

Exceptions

Exception Type Condition
ObjectDisposedException The underlying Socket has been closed.
SocketException An error occurred when accessing the socket. See the Remarks section for more information.

Remarks

The Receive method will block until a datagram arrives from a remote host. When data is available, the Receive method will read the first enqueued datagram and return the data portion as a byte array. This method populates the remoteEP parameter with the IPAddress and port number of the sender.

If you specify a default remote host in the Connect method, the Receive method will accept datagrams from that host only. All other datagrams will be discarded.

If the datagram you receive is larger than the size of the buffer parameter, buffer gets filled with the first part of the message, and the Receive method throws a SocketException. The remaining data is discarded. If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.

Note   If you intend to receive multicasted datagrams, do not call the Connect method prior to calling the Receive method. The UdpClient you use to receive datagrams must be created using the multicast port number.

Example

[Visual Basic, C#, C++] The following example demonstrates the Receive method. The Receive method blocks execution until it receives a message. Using the IPEndPoint passed to Receive, the identity of the responding host is revealed.

[Visual Basic] 
'Creates a UdpClient for reading incoming data.
Dim receivingUdpClient As New UdpClient()

'Creates an IPEndPoint to record the IP address and port number of the sender. 
' The IPEndPoint will allow you to read datagrams sent from any source.
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Try
   
   ' Blocks until a message returns on this socket from a remote host.
   Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint)
   
   Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
   
   Console.WriteLine(("This is the message you received " + returnData.ToString()))
   Console.WriteLine(("This message was sent from " + RemoteIpEndPoint.Address.ToString() + " on their port number " + RemoteIpEndPoint.Port.ToString()))
Catch e As Exception
   Console.WriteLine(e.ToString())
End Try
   End Sub 'MyUdpClientCommunicator
    

[C#] 
//Creates a UdpClient for reading incoming data.
UdpClient receivingUdpClient = new UdpClient();

//Creates an IPEndPoint to record the IP Address and port number of the sender. 
  // The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
try{

    // Blocks until a message returns on this socket from a remote host.
    Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); 

    string returnData = Encoding.ASCII.GetString(receiveBytes);

    Console.WriteLine("This is the message you received " +
                                 returnData.ToString());
    Console.WriteLine("This message was sent from " +
                                RemoteIpEndPoint.Address.ToString() +
                                " on their port number " +
                                RemoteIpEndPoint.Port.ToString());
}
catch ( Exception e ){
    Console.WriteLine(e.ToString()); 
}


[C++] 
//Creates a UdpClient for reading incoming data.
UdpClient* receivingUdpClient = new UdpClient();

//Creates an IPEndPoint to record the IP Address and port number of the sender. 
  // The IPEndPoint will allow you to read datagrams sent from any source.
IPEndPoint* RemoteIpEndPoint = new IPEndPoint(IPAddress::Any, 0);
try{

    // Blocks until a message returns on this socket from a remote host.
    Byte receiveBytes[] = receivingUdpClient->Receive(&RemoteIpEndPoint); 

    String* returnData = Encoding::ASCII->GetString(receiveBytes);

    Console::WriteLine(S"This is the message you received {0}", returnData);
    Console::WriteLine(S"This message was sent from {0} on their port number {1}", RemoteIpEndPoint->Address, __box(RemoteIpEndPoint->Port));
}
catch ( Exception* e ){
    Console::WriteLine(e->ToString()); 
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

UdpClient Class | UdpClient Members | System.Net.Sockets Namespace | Send | IPEndPoint | Connect | IPAddress

Show:
© 2015 Microsoft