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);
    }
}

注解

BeginSend异步操作必须通过调用 EndSend 方法来完成。 通常,委托会调用 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);
    }
}

注解

BeginSend异步操作必须通过调用 EndSend 方法来完成。 通常,委托会调用 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);
    }
}

注解

BeginSend异步操作必须通过调用 EndSend 方法来完成。 通常,委托会调用 requestCallback 方法。

在操作完成之前,此方法不会阻止。 若要在操作完成之前阻止,请使用 方法重载之 Send 一。

有关使用异步编程模型的详细信息,请参阅 异步调用同步方法

适用于