UdpClient 클래스

정의

UDP(User Datagram Protocol) 네트워크 서비스를 제공합니다.

public ref class UdpClient : IDisposable
public class UdpClient : IDisposable
type UdpClient = class
    interface IDisposable
Public Class UdpClient
Implements IDisposable
상속
UdpClient
구현

예제

다음 예제에서는 UdpClient 포트 11000의 호스트 이름을 www.contoso.com 사용하여 연결을 설정합니다. 작은 문자열 메시지는 두 개의 별도 원격 호스트 컴퓨터로 전송됩니다. 메서드는 Receive 메시지를 받을 때까지 실행을 차단합니다. IPEndPoint 에 전달된 를 Receive사용하여 응답 호스트의 ID가 표시됩니다.

// With this constructor the local port number is arbitrarily assigned.
UdpClient^ udpClient = gcnew UdpClient;
try
{
   udpClient->Connect( "host.contoso.com", 11000 );

   // Send message to the host to which you have connected.
   array<Byte>^sendBytes = Encoding::ASCII->GetBytes( "Is anybody there?" );
   udpClient->Send( sendBytes, sendBytes->Length );

   // Send message to a different host using optional hostname and port parameters.
   UdpClient^ udpClientB = gcnew UdpClient;
   udpClientB->Send( sendBytes, sendBytes->Length, "AlternateHostMachineName", 11000 );

   //IPEndPoint object will allow us to read datagrams sent from any source.
   IPEndPoint^ RemoteIpEndPoint = gcnew IPEndPoint( IPAddress::Any,0 );

   // Block until a message returns on this socket from a remote host.
   array<Byte>^receiveBytes = udpClient->Receive( RemoteIpEndPoint );
   String^ returnData = Encoding::ASCII->GetString( receiveBytes );

   // Use the IPEndPoint object to determine which of these two hosts responded.
   Console::WriteLine( String::Concat( "This is the message you received ", returnData->ToString() ) );
   Console::WriteLine( String::Concat( "This message was sent from ", RemoteIpEndPoint->Address->ToString(), " on their port number ", RemoteIpEndPoint->Port.ToString() ) );
   udpClient->Close();
   udpClientB->Close();
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient(11000);
    try{
         udpClient.Connect("www.contoso.com", 11000);

         // Sends a message to the host to which you have connected.
         Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");

         udpClient.Send(sendBytes, sendBytes.Length);

         // Sends a message to a different host using optional hostname and port parameters.
         UdpClient udpClientB = new UdpClient();
         udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000);

         //IPEndPoint object will allow us to read datagrams sent from any source.
         IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);

         // Blocks until a message returns on this socket from a remote host.
         Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
         string returnData = Encoding.ASCII.GetString(receiveBytes);

         // Uses the IPEndPoint object to determine which of these two hosts responded.
         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());

          udpClient.Close();
          udpClientB.Close();
          }
       catch (Exception e ) {
                  Console.WriteLine(e.ToString());
        }
     ' This constructor arbitrarily assigns the local port number.
     Dim udpClient As New UdpClient(11000)
     Try
        udpClient.Connect("www.contoso.com", 11000)
        
        ' Sends a message to the host to which you have connected.
        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
        
        udpClient.Send(sendBytes, sendBytes.Length)
        
        ' Sends message to a different host using optional hostname and port parameters.
        Dim udpClientB As New UdpClient()
        udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000)
        
        ' IPEndPoint object will allow us to read datagrams sent from any source.
        Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
        
        ' UdpClient.Receive blocks until a message is received from a remote host.
        Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
        Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
        
        ' Which one of these two hosts responded?
        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()))
        udpClient.Close()
        udpClientB.Close()

     Catch e As Exception
        Console.WriteLine(e.ToString())
     End Try
  End Sub

설명

클래스는 UdpClient 동기 모드 차단에서 연결 없는 UDP 데이터그램을 보내고 받기 위한 간단한 메서드를 제공합니다. UDP는 연결 없는 전송 프로토콜이므로 데이터를 보내고 받기 전에 원격 호스트 연결을 설정할 필요가 없습니다. 그러나 다음 두 가지 방법 중 하나로 기본 원격 호스트를 설정할 수 있습니다.

  • 원격 호스트 이름 및 포트 번호를 매개 변수로 사용하여 클래스의 UdpClient 인스턴스를 만듭니다.

  • 클래스의 인스턴스를 UdpClient 만든 다음 메서드를 호출합니다 Connect .

제공 하는 송신 메서드 중 하나를 사용할 수는 UdpClient 원격 디바이스에 데이터를 보내도록 합니다. 메서드를 Receive 사용하여 원격 호스트에서 데이터를 받습니다.

참고

호스트 이름을 사용하거나 IPEndPoint 기본 원격 호스트를 이미 지정한 경우 를 호출 Send 하지 마세요. 이렇게 하면 예외가 UdpClient throw됩니다.

UdpClient 메서드를 사용하면 멀티캐스트 데이터그램을 보내고 받을 수도 있습니다. 메서드를 JoinMulticastGroup 사용하여 를 멀티캐스트 그룹에 구독 UdpClient 합니다. 메서드를 DropMulticastGroup 사용하여 멀티캐스트 그룹에서 을 UdpClient 구독 취소합니다.

생성자

UdpClient()

UdpClient 클래스의 새 인스턴스를 초기화합니다.

UdpClient(AddressFamily)

UdpClient 클래스의 새 인스턴스를 초기화합니다.

UdpClient(Int32)

UdpClient 클래스의 새 인스턴스를 초기화하고 해당 인스턴스를 제공되는 로컬 포트 번호에 바인딩합니다.

UdpClient(Int32, AddressFamily)

UdpClient 클래스의 새 인스턴스를 초기화하고 해당 인스턴스를 제공되는 로컬 포트 번호에 바인딩합니다.

UdpClient(IPEndPoint)

UdpClient 클래스의 새 인스턴스를 초기화하고 해당 인스턴스를 지정된 로컬 엔드포인트에 바인딩합니다.

UdpClient(String, Int32)

UdpClient 클래스의 새 인스턴스를 초기화하고 기본 원격 호스트를 설정합니다.

속성

Active

기본 원격 호스트가 설정되었는지 여부를 나타내는 값을 가져오거나 설정합니다.

Available

네트워크에서 받은 데이터 중 읽을 수 있는 데이터의 양을 가져옵니다.

Client

내부 네트워크 Socket을 가져오거나 설정합니다.

DontFragment

Boolean에서 IP(인터넷 프로토콜) 데이터그램의 조각화를 허용하는지 여부를 나타내는 UdpClient 값을 가져오거나 설정합니다.

EnableBroadcast

가 브로드캐스트 패킷을 Boolean 보낼 수 있는지 여부를 UdpClient 지정하는 값을 가져오거나 설정합니다.

ExclusiveAddressUse

Boolean가 하나의 포트를 하나의 클라이언트에서만 사용하도록 허용하는지 여부를 지정하는 UdpClient 값을 가져오거나 설정합니다.

MulticastLoopback

나가는 멀티캐스트 패킷을 보내는 애플리케이션에 전달할지 여부를 지정하는 Boolean 값을 가져오거나 설정합니다.

Ttl

UdpClient에서 보낸 IP(인터넷 프로토콜) 패킷의 TTL(Time to Live) 값을 지정하는 값을 가져오거나 설정합니다.

메서드

AllowNatTraversal(Boolean)

UdpClient 인스턴스에 대해 NAT(Network Address Translation) 통과를 설정하거나 해제합니다.

BeginReceive(AsyncCallback, Object)

데이터그램을 원격 호스트에서 비동기적으로 받습니다.

BeginSend(Byte[], Int32, AsyncCallback, Object)

데이터그램을 원격 호스트에 비동기적으로 보냅니다. 대상은 이전에 Connect를 호출할 때 지정됩니다.

BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object)

데이터그램을 대상에 비동기적으로 보냅니다. 대상은 EndPoint에 지정됩니다.

BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object)

데이터그램을 대상에 비동기적으로 보냅니다. 대상은 호스트 이름과 포트 번호를 사용하여 지정합니다.

Close()

UDP 연결을 닫습니다.

Connect(IPAddress, Int32)

지정된 IP 주소와 포트 번호를 사용하여 기본 원격 호스트를 설정합니다.

Connect(IPEndPoint)

지정된 네트워크 엔드포인트를 사용하여 기본 원격 호스트를 설정합니다.

Connect(String, Int32)

지정된 호스트 이름과 포트 번호를 사용하여 기본 원격 호스트를 설정합니다.

Dispose()

UdpClient에서 사용하는 관리되는 리소스 및 관리되지 않는 리소스를 해제합니다.

Dispose(Boolean)

UdpClient에서 사용하는 관리되지 않는 리소스를 해제하고, 관리되는 리소스를 선택적으로 해제할 수 있습니다.

DropMulticastGroup(IPAddress)

멀티캐스트 그룹을 나갑니다.

DropMulticastGroup(IPAddress, Int32)

멀티캐스트 그룹을 나갑니다.

EndReceive(IAsyncResult, IPEndPoint)

보류 중인 비동기 수신을 끝냅니다.

EndSend(IAsyncResult)

보류 중인 비동기 보내기를 끝냅니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
JoinMulticastGroup(Int32, IPAddress)

멀티캐스트 그룹에 UdpClient를 추가합니다.

JoinMulticastGroup(IPAddress)

멀티캐스트 그룹에 UdpClient를 추가합니다.

JoinMulticastGroup(IPAddress, Int32)

지정된 TTL(Time to Live)을 사용하여 멀티캐스트 그룹에 UdpClient를 추가합니다.

JoinMulticastGroup(IPAddress, IPAddress)

멀티캐스트 그룹에 UdpClient를 추가합니다.

MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
Receive(IPEndPoint)

원격 호스트가 보낸 UDP 데이터그램을 반환합니다.

ReceiveAsync()

원격 호스트가 보낸 UDP 데이터그램을 비동기적으로 반환합니다.

ReceiveAsync(CancellationToken)

원격 호스트가 보낸 UDP 데이터그램을 비동기적으로 반환합니다.

Send(Byte[], Int32)

원격 호스트에 UDP 데이터그램을 보냅니다.

Send(Byte[], Int32, IPEndPoint)

지정된 원격 엔드포인트에서 호스트에 UDP 데이터그램을 보냅니다.

Send(Byte[], Int32, String, Int32)

지정된 원격 호스트의 지정된 포트에 UDP 데이터그램을 보냅니다.

Send(ReadOnlySpan<Byte>)

원격 호스트에 UDP 데이터그램을 보냅니다.

Send(ReadOnlySpan<Byte>, IPEndPoint)

지정된 원격 엔드포인트에서 호스트에 UDP 데이터그램을 보냅니다.

Send(ReadOnlySpan<Byte>, String, Int32)

지정된 원격 호스트의 지정된 포트에 UDP 데이터그램을 보냅니다.

SendAsync(Byte[], Int32)

UDP 데이터그램을 원격 호스트에 비동기적으로 보냅니다.

SendAsync(Byte[], Int32, IPEndPoint)

UDP 데이터그램을 원격 호스트에 비동기적으로 보냅니다.

SendAsync(Byte[], Int32, String, Int32)

UDP 데이터그램을 원격 호스트에 비동기적으로 보냅니다.

SendAsync(ReadOnlyMemory<Byte>, CancellationToken)

UDP 데이터그램을 원격 호스트에 비동기적으로 보냅니다.

SendAsync(ReadOnlyMemory<Byte>, IPEndPoint, CancellationToken)

UDP 데이터그램을 원격 호스트에 비동기적으로 보냅니다.

SendAsync(ReadOnlyMemory<Byte>, String, Int32, CancellationToken)

UDP 데이터그램을 원격 호스트에 비동기적으로 보냅니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

IDisposable.Dispose()

이 API는 제품 인프라를 지원하며 코드에서 직접 사용되지 않습니다.

UdpClient에서 사용하는 모든 리소스를 해제합니다.

적용 대상

추가 정보