UdpClient.BeginSend 메서드

정의

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

오버로드

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

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

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

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

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

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

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

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

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

public:
 IAsyncResult ^ BeginSend(cli::array <System::Byte> ^ datagram, int bytes, System::String ^ hostname, int port, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginSend (byte[] datagram, int bytes, string? hostname, int port, AsyncCallback? requestCallback, object? state);
public IAsyncResult BeginSend (byte[] datagram, int bytes, string hostname, int port, AsyncCallback requestCallback, object state);
member this.BeginSend : byte[] * int * string * int * AsyncCallback * obj -> IAsyncResult
Public Function BeginSend (datagram As Byte(), bytes As Integer, hostname As String, port As Integer, requestCallback As AsyncCallback, state As Object) As IAsyncResult

매개 변수

datagram
Byte[]

보낼 데이터가 들어 있는 Byte 배열입니다.

bytes
Int32

보낼 바이트 수입니다.

hostname
String

대상 호스트입니다.

port
Int32

대상 포트 번호입니다.

requestCallback
AsyncCallback

작업이 완료되었을 때 호출할 메서드를 참조하는 AsyncCallback 대리자입니다.

state
Object

보내기 작업에 대한 정보가 들어 있는 사용자 정의 개체입니다. 이 개체는 작업이 완료되면 requestCallback 대리자에게 전달됩니다.

반환

비동기 보내기를 참조하는 IAsyncResult 개체입니다.

예제

다음 코드 예제에서는 를 사용하여 BeginSend 서버 요청을 비동기적으로 보냅니다.

public:
    static bool isMessageSent;

    static void SendCallback(IAsyncResult^ asyncResult)
    {
        UdpClient^ udpClient = (UdpClient^)asyncResult->AsyncState;

        Console::WriteLine("number of bytes sent: {0}",
            udpClient->EndSend(asyncResult));
        isMessageSent = true;
    }
public static bool messageSent = false;

public static void SendCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)ar.AsyncState;

    Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
    messageSent = true;
}
public:
    static void SendMessage3(String^ server, String^ message)
    {
        // create the udp socket
        UdpClient^ udpClient = gcnew UdpClient();

        array<Byte>^ sendBytes = Encoding::ASCII->GetBytes(message);

        // send the message
        // the destination is defined by the server name and port
        udpClient->BeginSend(sendBytes, sendBytes->Length, server, listenPort,
            gcnew AsyncCallback(SendCallback), udpClient);

        // Do some work while we wait for the send to complete. For
        // this example, we'll just sleep
        while (!isMessageSent)
        {
            Thread::Sleep(100);
        }
    }
static void SendMessage3(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();

    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // send the message
    // the destination is defined by the server name and port
    u.BeginSend(sendBytes, sendBytes.Length, server, s_listenPort, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}

설명

메서드를 호출 EndSend 하여 비동 BeginSend 기 작업을 완료해야 합니다. 일반적으로 메서드는 대리자에서 호출됩니다 requestCallback .

이 메서드는 작업이 완료될 때까지 차단되지 않습니다. 작업이 완료될 때까지 차단하려면 메서드 오버로드 중 Send 하나를 사용합니다.

비동기 프로그래밍 모델 사용에 대한 자세한 내용은 동기 메서드 비동기 호출을 참조하세요.

적용 대상

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

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

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

public:
 IAsyncResult ^ BeginSend(cli::array <System::Byte> ^ datagram, int bytes, System::Net::IPEndPoint ^ endPoint, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginSend (byte[] datagram, int bytes, System.Net.IPEndPoint? endPoint, AsyncCallback? requestCallback, object? state);
public IAsyncResult BeginSend (byte[] datagram, int bytes, System.Net.IPEndPoint endPoint, AsyncCallback requestCallback, object state);
member this.BeginSend : byte[] * int * System.Net.IPEndPoint * AsyncCallback * obj -> IAsyncResult
Public Function BeginSend (datagram As Byte(), bytes As Integer, endPoint As IPEndPoint, requestCallback As AsyncCallback, state As Object) As IAsyncResult

매개 변수

datagram
Byte[]

보낼 데이터가 들어 있는 Byte 배열입니다.

bytes
Int32

보낼 바이트 수입니다.

endPoint
IPEndPoint

데이터의 대상을 나타내는 EndPoint입니다.

requestCallback
AsyncCallback

작업이 완료되었을 때 호출할 메서드를 참조하는 AsyncCallback 대리자입니다.

state
Object

보내기 작업에 대한 정보가 들어 있는 사용자 정의 개체입니다. 이 개체는 작업이 완료되면 requestCallback 대리자에게 전달됩니다.

반환

비동기 보내기를 참조하는 IAsyncResult 개체입니다.

예제

다음 코드 예제에서는 를 사용하여 BeginSend 서버 요청을 비동기적으로 보냅니다.

public:
    static bool isMessageSent;

    static void SendCallback(IAsyncResult^ asyncResult)
    {
        UdpClient^ udpClient = (UdpClient^)asyncResult->AsyncState;

        Console::WriteLine("number of bytes sent: {0}",
            udpClient->EndSend(asyncResult));
        isMessageSent = true;
    }
public static bool messageSent = false;

public static void SendCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)ar.AsyncState;

    Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
    messageSent = true;
}
public:
    static void SendMessage2(String^ server, String^ message)
    {
        // create the udp socket
        UdpClient^ udpClient = gcnew UdpClient();
        array<Byte>^ sendBytes = Encoding::ASCII->GetBytes(message);

        // resolve the server name
        IPHostEntry^ resolvedServer = Dns::GetHostEntry(server);

        IPEndPoint^ ipEndPoint =
            gcnew IPEndPoint(resolvedServer->AddressList[0], listenPort);

        // send the message
        // the destination is defined by the IPEndPoint
        udpClient->BeginSend(sendBytes, sendBytes->Length, ipEndPoint,
            gcnew AsyncCallback(SendCallback), udpClient);

        // Do some work while we wait for the send to complete. For
        // this example, we'll just sleep
        while (!isMessageSent)
        {
            Thread::Sleep(100);
        }
    }
static void SendMessage2(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();
    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // resolve the server name
    IPHostEntry heserver = Dns.GetHostEntry(server);

    IPEndPoint e = new IPEndPoint(heserver.AddressList[0], s_listenPort);

    // send the message
    // the destination is defined by the IPEndPoint
    u.BeginSend(sendBytes, sendBytes.Length, e, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}

설명

메서드를 호출 EndSend 하여 비동 BeginSend 기 작업을 완료해야 합니다. 일반적으로 메서드는 대리자에서 호출됩니다 requestCallback .

이 메서드는 작업이 완료될 때까지 차단되지 않습니다. 작업이 완료될 때까지 차단하려면 메서드 오버로드 중 Send 하나를 사용합니다.

비동기 프로그래밍 모델 사용에 대한 자세한 내용은 동기 메서드 비동기 호출을 참조하세요.

적용 대상

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

Source:
UDPClient.cs
Source:
UDPClient.cs
Source:
UDPClient.cs

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

public:
 IAsyncResult ^ BeginSend(cli::array <System::Byte> ^ datagram, int bytes, AsyncCallback ^ requestCallback, System::Object ^ state);
public IAsyncResult BeginSend (byte[] datagram, int bytes, AsyncCallback? requestCallback, object? state);
public IAsyncResult BeginSend (byte[] datagram, int bytes, AsyncCallback requestCallback, object state);
member this.BeginSend : byte[] * int * AsyncCallback * obj -> IAsyncResult
Public Function BeginSend (datagram As Byte(), bytes As Integer, requestCallback As AsyncCallback, state As Object) As IAsyncResult

매개 변수

datagram
Byte[]

보낼 데이터가 들어 있는 Byte 배열입니다.

bytes
Int32

보낼 바이트 수입니다.

requestCallback
AsyncCallback

작업이 완료되었을 때 호출할 메서드를 참조하는 AsyncCallback 대리자입니다.

state
Object

보내기 작업에 대한 정보가 들어 있는 사용자 정의 개체입니다. 이 개체는 작업이 완료되면 requestCallback 대리자에게 전달됩니다.

반환

비동기 보내기를 참조하는 IAsyncResult 개체입니다.

예제

다음 코드 예제에서는 를 사용하여 BeginSend 서버 요청을 비동기적으로 보냅니다.

public:
    static bool isMessageSent;

    static void SendCallback(IAsyncResult^ asyncResult)
    {
        UdpClient^ udpClient = (UdpClient^)asyncResult->AsyncState;

        Console::WriteLine("number of bytes sent: {0}",
            udpClient->EndSend(asyncResult));
        isMessageSent = true;
    }
public static bool messageSent = false;

public static void SendCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)ar.AsyncState;

    Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
    messageSent = true;
}
public:
    static void SendMessage1(String^ server, String^ message)
    {
        // create the udp socket
        UdpClient^ udpClient = gcnew UdpClient();

        udpClient->Connect(server, listenPort);
        array<Byte>^ sendBytes = Encoding::ASCII->GetBytes(message);

        // send the message
        // the destination is defined by the call to .Connect()
        udpClient->BeginSend(sendBytes, sendBytes->Length,
            gcnew AsyncCallback(SendCallback), udpClient);

        // Do some work while we wait for the send to complete. For
        // this example, we'll just sleep
        while (!isMessageSent)
        {
            Thread::Sleep(100);
        }
    }
static void SendMessage1(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();

    u.Connect(server, s_listenPort);
    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // send the message
    // the destination is defined by the call to .Connect()
    u.BeginSend(sendBytes, sendBytes.Length, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}

설명

메서드를 호출 EndSend 하여 비동 BeginSend 기 작업을 완료해야 합니다. 일반적으로 메서드는 대리자에서 호출됩니다 requestCallback .

이 메서드는 작업이 완료될 때까지 차단되지 않습니다. 작업이 완료될 때까지 차단하려면 메서드 오버로드 중 Send 하나를 사용합니다.

비동기 프로그래밍 모델 사용에 대한 자세한 내용은 동기 메서드 비동기 호출을 참조하세요.

적용 대상