UdpClient.BeginSend Metodo

Definizione

Invia un datagramma a un host remoto in modo asincrono.

Overload

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

Invia un datagramma a un host remoto in modo asincrono. La destinazione è stata specificata in precedenza da una chiamata a Connect.

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

Invia un datagramma a una destinazione in modo asincrono. La destinazione viene specificata da un oggetto EndPoint.

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

Invia un datagramma a una destinazione in modo asincrono. La destinazione viene specificata dal nome host e dal numero di porta.

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

Invia un datagramma a un host remoto in modo asincrono. La destinazione è stata specificata in precedenza da una chiamata a 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);
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

Parametri

datagram
Byte[]

Matrice Byte che contiene i dati da inviare.

bytes
Int32

Numero di byte da inviare.

requestCallback
AsyncCallback

Delegato AsyncCallback che fa riferimento al metodo da richiamare al termine dell'operazione.

state
Object

Oggetto definito dall'utente che contiene informazioni sull'operazione di invio. Questo oggetto viene passato al requestCallback delegato al termine dell'operazione.

Valori restituiti

Oggetto IAsyncResult che fa riferimento all'invio asincrono.

Esempio

Nell'esempio di codice seguente viene BeginSend usato per inviare in modo asincrono una richiesta del server.

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

Commenti

L'operazione asincrona BeginSend deve essere completata chiamando il EndSend metodo . In genere, il metodo viene richiamato dal requestCallback delegato.

Questo metodo non viene bloccato fino al completamento dell'operazione. Per bloccare fino al completamento dell'operazione, usare uno degli overload del Send metodo.

Per informazioni dettagliate sull'uso del modello di programmazione asincrona, vedere Chiamata asincrona di metodi sincroni.

Si applica a

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

Invia un datagramma a una destinazione in modo asincrono. La destinazione viene specificata da un oggetto 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);
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

Parametri

datagram
Byte[]

Matrice Byte che contiene i dati da inviare.

bytes
Int32

Numero di byte da inviare.

endPoint
IPEndPoint

Oggetto EndPoint che rappresenta la destinazione per i dati.

requestCallback
AsyncCallback

Delegato AsyncCallback che fa riferimento al metodo da richiamare al termine dell'operazione.

state
Object

Oggetto definito dall'utente che contiene informazioni sull'operazione di invio. Questo oggetto viene passato al requestCallback delegato al termine dell'operazione.

Valori restituiti

Oggetto IAsyncResult che fa riferimento all'invio asincrono.

Esempio

Nell'esempio di codice seguente viene BeginSend usato per inviare in modo asincrono una richiesta del server.

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

Commenti

L'operazione asincrona BeginSend deve essere completata chiamando il EndSend metodo . In genere, il metodo viene richiamato dal requestCallback delegato.

Questo metodo non viene bloccato fino al completamento dell'operazione. Per bloccare fino al completamento dell'operazione, usare uno degli overload del Send metodo.

Per informazioni dettagliate sull'uso del modello di programmazione asincrona, vedere Chiamata asincrona di metodi sincroni.

Si applica a

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

Invia un datagramma a una destinazione in modo asincrono. La destinazione viene specificata dal nome host e dal numero di 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);
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

Parametri

datagram
Byte[]

Matrice Byte che contiene i dati da inviare.

bytes
Int32

Numero di byte da inviare.

hostname
String

Host di destinazione.

port
Int32

Numero di porta di destinazione.

requestCallback
AsyncCallback

Delegato AsyncCallback che fa riferimento al metodo da richiamare al termine dell'operazione.

state
Object

Oggetto definito dall'utente che contiene informazioni sull'operazione di invio. Questo oggetto viene passato al requestCallback delegato al termine dell'operazione.

Valori restituiti

Oggetto IAsyncResult che fa riferimento all'invio asincrono.

Esempio

Nell'esempio di codice seguente viene BeginSend usato per inviare in modo asincrono una richiesta del server.

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

Commenti

L'operazione asincrona BeginSend deve essere completata chiamando il EndSend metodo . In genere, il metodo viene richiamato dal requestCallback delegato.

Questo metodo non viene bloccato fino al completamento dell'operazione. Per bloccare fino al completamento dell'operazione, usare uno degli overload del Send metodo.

Per informazioni dettagliate sull'uso del modello di programmazione asincrona, vedere Chiamata asincrona di metodi sincroni.

Si applica a