UdpClient.Receive(IPEndPoint) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回由远程主机发送的 UDP 数据报。
public:
cli::array <System::Byte> ^ Receive(System::Net::IPEndPoint ^ % remoteEP);
public byte[] Receive (ref System.Net.IPEndPoint? remoteEP);
public byte[] Receive (ref System.Net.IPEndPoint remoteEP);
member this.Receive : IPEndPoint -> byte[]
Public Function Receive (ByRef remoteEP As IPEndPoint) As Byte()
参数
- remoteEP
- IPEndPoint
一个 IPEndPoint,它表示从其发送数据的远程主机。
返回
Byte[]
一个 Byte 类型的数组,它包含数据报数据。
例外
已关闭基础 Socket。
访问套接字时出错。
示例
下面的示例演示 Receive 方法。 方法 Receive 将阻止执行,直到收到消息。 IPEndPoint使用传递给 Receive的 显示响应主机的标识。
//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() );
}
//Creates a UdpClient for reading incoming data.
UdpClient receivingUdpClient = new 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 = 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());
}
'Creates a UdpClient for reading incoming data.
Dim receivingUdpClient As New 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.
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
注解
方法 Receive 将阻止,直到数据报从远程主机到达。 当数据可用时, Receive 方法将读取第一个排队的数据报,并将数据部分作为字节数组返回。 此方法使用remoteEP
IPAddress发送方的 和 端口号填充 参数。
如果在 方法中 Connect 指定默认远程主机,该方法 Receive 将仅接受来自该主机的数据报。 将丢弃所有其他数据报。
如果收到 SocketException,请使用 SocketException.ErrorCode 获取特定的错误代码。 获取此代码后,可以参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。