UdpClient.BeginSend Método

Definição

Envia um datagrama para um host remoto de forma assíncrona.

Sobrecargas

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

Envia um datagrama para um destino de forma assíncrona. O destino é especificado pelo nome do host e número da porta.

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

Envia um datagrama para um destino de forma assíncrona. O destino é especificado por um EndPoint.

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

Envia um datagrama para um host remoto de forma assíncrona. O destino foi especificado anteriormente por uma chamada para Connect.

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

Origem:
UDPClient.cs
Origem:
UDPClient.cs
Origem:
UDPClient.cs

Envia um datagrama para um destino de forma assíncrona. O destino é especificado pelo nome do host e número da porta.

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

Parâmetros

datagram
Byte[]

Uma matriz Byte que contém os dados a serem enviados.

bytes
Int32

O número de bytes a serem enviados.

hostname
String

O host de destino.

port
Int32

O número da porta de destino.

requestCallback
AsyncCallback

Um delegado AsyncCallback que faz referência ao método a ser invocado quando a operação é concluída.

state
Object

Um objeto definido pelo usuário que contém informações sobre a operação de envio. Esse objeto é passado para o delegado requestCallback quando a operação é concluída.

Retornos

Um objeto IAsyncResult que faz referência ao envio assíncrono.

Exemplos

O exemplo de código a seguir usa BeginSend para enviar de forma assíncrona uma solicitação de servidor.

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

Comentários

A operação assíncrona BeginSend deve ser concluída chamando o EndSend método . Normalmente, o método é invocado pelo requestCallback delegado.

Esse método não é bloqueado até que a operação seja concluída. Para bloquear até que a operação seja concluída, use uma das sobrecargas de Send método.

Para obter informações detalhadas sobre como usar o modelo de programação assíncrona, consulte Chamando métodos síncronos de forma assíncrona.

Aplica-se a

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

Origem:
UDPClient.cs
Origem:
UDPClient.cs
Origem:
UDPClient.cs

Envia um datagrama para um destino de forma assíncrona. O destino é especificado por um 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

Parâmetros

datagram
Byte[]

Uma matriz Byte que contém os dados a serem enviados.

bytes
Int32

O número de bytes a serem enviados.

endPoint
IPEndPoint

O EndPoint que representa o destino dos dados.

requestCallback
AsyncCallback

Um delegado AsyncCallback que faz referência ao método a ser invocado quando a operação é concluída.

state
Object

Um objeto definido pelo usuário que contém informações sobre a operação de envio. Esse objeto é passado para o delegado requestCallback quando a operação é concluída.

Retornos

Um objeto IAsyncResult que faz referência ao envio assíncrono.

Exemplos

O exemplo de código a seguir usa BeginSend para enviar de forma assíncrona uma solicitação de servidor.

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

Comentários

A operação assíncrona BeginSend deve ser concluída chamando o EndSend método . Normalmente, o método é invocado pelo requestCallback delegado.

Esse método não é bloqueado até que a operação seja concluída. Para bloquear até que a operação seja concluída, use uma das sobrecargas de Send método.

Para obter informações detalhadas sobre como usar o modelo de programação assíncrona, consulte Chamando métodos síncronos de forma assíncrona.

Aplica-se a

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

Origem:
UDPClient.cs
Origem:
UDPClient.cs
Origem:
UDPClient.cs

Envia um datagrama para um host remoto de forma assíncrona. O destino foi especificado anteriormente por uma chamada para 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

Parâmetros

datagram
Byte[]

Uma matriz Byte que contém os dados a serem enviados.

bytes
Int32

O número de bytes a serem enviados.

requestCallback
AsyncCallback

Um delegado AsyncCallback que faz referência ao método a ser invocado quando a operação é concluída.

state
Object

Um objeto definido pelo usuário que contém informações sobre a operação de envio. Esse objeto é passado para o delegado requestCallback quando a operação é concluída.

Retornos

Um objeto IAsyncResult que faz referência ao envio assíncrono.

Exemplos

O exemplo de código a seguir usa BeginSend para enviar de forma assíncrona uma solicitação de servidor.

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

Comentários

A operação assíncrona BeginSend deve ser concluída chamando o EndSend método . Normalmente, o método é invocado pelo requestCallback delegado.

Esse método não é bloqueado até que a operação seja concluída. Para bloquear até que a operação seja concluída, use uma das sobrecargas de Send método.

Para obter informações detalhadas sobre como usar o modelo de programação assíncrona, consulte Chamando métodos síncronos de forma assíncrona.

Aplica-se a