UdpClient::Receive Method (IPEndPoint^%)
Returns a UDP datagram that was sent by a remote host.
Assembly: System (in System.dll)
Parameters
- remoteEP
-
Type:
System.Net::IPEndPoint^%
An IPEndPoint that represents the remote host from which the data was sent.
| Exception | Condition |
|---|---|
| ObjectDisposedException | The underlying Socket has been closed. |
| SocketException | An error occurred when accessing the socket. See the Remarks section for more information. |
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 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 Sockets version 2 API error code documentation in MSDN for a detailed description of the error.
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.
//Creates a UdpClient for reading incoming data. UdpClient^ receivingUdpClient = gcnew UdpClient( 11000 ); //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 = gcnew IPEndPoint( IPAddress::Any,0 ); try { // Blocks until a message returns on this socket from a remote host. array<Byte>^receiveBytes = receivingUdpClient->Receive( RemoteIpEndPoint ); String^ returnData = Encoding::ASCII->GetString( receiveBytes ); Console::WriteLine( "This is the message you received {0}", returnData ); Console::WriteLine( "This message was sent from {0} on their port number {1}", RemoteIpEndPoint->Address, RemoteIpEndPoint->Port ); } catch ( Exception^ e ) { Console::WriteLine( e->ToString() ); }
Available since 1.1
