UdpClient.Receive(IPEndPoint) 메서드

정의

원격 호스트에서 보낸 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 메시지를 받을 때까지 실행을 차단합니다. 전달을 IPEndPointReceive사용하여 응답 호스트의 ID가 표시됩니다.

 //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 오류 코드 설명서에서 오류에 대한 자세한 설명을 참조할 수 있습니다.

메모

멀티캐스트된 데이터그램을 받으려는 경우 메서드를 호출하기 전에 메서드를 호출 ConnectReceive 하지 마세요. UdpClient 멀티캐스트 포트 번호를 사용하여 데이터그램을 수신하는 데 사용해야 합니다.

적용 대상

추가 정보