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)

ソース:
UDPClient.cs
ソース:
UDPClient.cs
ソース:
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);
    }
}

注釈

非同期 BeginSend 操作は、 メソッドを呼び出して完了する EndSend 必要があります。 通常、 メソッドはデリゲートによって requestCallback 呼び出されます。

このメソッドは、操作が完了するまでブロックしません。 操作が完了するまでブロックするには、いずれかのメソッド オーバーロードを Send 使用します。

非同期プログラミング モデルの使用の詳細については、「 同期メソッドの非同期呼び出し」を参照してください。

適用対象

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

ソース:
UDPClient.cs
ソース:
UDPClient.cs
ソース:
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);
    }
}

注釈

非同期 BeginSend 操作は、 メソッドを呼び出して完了する EndSend 必要があります。 通常、 メソッドはデリゲートによって requestCallback 呼び出されます。

このメソッドは、操作が完了するまでブロックしません。 操作が完了するまでブロックするには、いずれかのメソッド オーバーロードを Send 使用します。

非同期プログラミング モデルの使用の詳細については、「 同期メソッドの非同期呼び出し」を参照してください。

適用対象

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

ソース:
UDPClient.cs
ソース:
UDPClient.cs
ソース:
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);
    }
}

注釈

非同期 BeginSend 操作は、 メソッドを呼び出して完了する EndSend 必要があります。 通常、 メソッドはデリゲートによって requestCallback 呼び出されます。

このメソッドは、操作が完了するまでブロックしません。 操作が完了するまでブロックするには、いずれかのメソッド オーバーロードを Send 使用します。

非同期プログラミング モデルの使用の詳細については、「 同期メソッドの非同期呼び出し」を参照してください。

適用対象