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

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

참고

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

적용 대상

추가 정보