UdpClient 클래스

정의

UDP(사용자 데이터그램 프로토콜) 네트워크 서비스를 제공합니다.

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

예제

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

// 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 합니다.

생성자

Name Description
UdpClient()

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

UdpClient(AddressFamily)

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

UdpClient(Int32, AddressFamily)

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

UdpClient(Int32)

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

UdpClient(IPEndPoint)

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

UdpClient(String, Int32)

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

속성

Name Description
Active

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

Available

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

Client

기본 네트워크를 Socket가져오거나 설정합니다.

DontFragment

IP(인터넷 프로토콜) 데이터그램을 조각화할 수 있는지 여부를 Boolean 지정하는 값을 가져오거나 설정합니다UdpClient.

EnableBroadcast

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

ExclusiveAddressUse

하나의 클라이언트만 포트를 Boolean 사용할 수 있는지 여부를 UdpClient 지정하는 값을 가져오거나 설정합니다.

MulticastLoopback

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

Ttl

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

메서드

Name Description
AllowNatTraversal(Boolean)

인스턴스에서 NAT(네트워크 주소 변환) 통과를 UdpClient 사용하거나 사용하지 않도록 설정합니다.

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, Int32)

멀티캐스트 그룹을 둡니다.

DropMulticastGroup(IPAddress)

멀티캐스트 그룹을 둡니다.

EndReceive(IAsyncResult, IPEndPoint)

보류 중인 비동기 수신을 종료합니다.

EndSend(IAsyncResult)

보류 중인 비동기 보내기를 종료합니다.

Equals(Object)

지정한 개체와 현재 개체가 같은지 여부를 확인합니다.

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

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

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

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

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

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

JoinMulticastGroup(IPAddress, Int32)

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

JoinMulticastGroup(IPAddress, IPAddress)

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

JoinMulticastGroup(IPAddress)

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

MemberwiseClone()

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

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

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

ReceiveAsync()

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

Send(Byte[], Int32, IPEndPoint)

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

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

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

Send(Byte[], Int32)

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

SendAsync(Byte[], Int32, IPEndPoint)

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

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

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

SendAsync(Byte[], Int32)

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

ToString()

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

(다음에서 상속됨 Object)

명시적 인터페이스 구현

Name Description
IDisposable.Dispose()

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

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

적용 대상

추가 정보