UdpClient.EndReceive(IAsyncResult, IPEndPoint) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Termina una ricezione asincrona in sospeso.
public:
cli::array <System::Byte> ^ EndReceive(IAsyncResult ^ asyncResult, System::Net::IPEndPoint ^ % remoteEP);
public byte[] EndReceive(IAsyncResult asyncResult, ref System.Net.IPEndPoint? remoteEP);
public byte[] EndReceive(IAsyncResult asyncResult, ref System.Net.IPEndPoint remoteEP);
member this.EndReceive : IAsyncResult * IPEndPoint -> byte[]
Public Function EndReceive (asyncResult As IAsyncResult, ByRef remoteEP As IPEndPoint) As Byte()
Parametri
- asyncResult
- IAsyncResult
Oggetto IAsyncResult restituito da una chiamata a BeginReceive(AsyncCallback, Object).
- remoteEP
- IPEndPoint
Endpoint remoto specificato.
Valori restituiti
In caso di esito positivo, matrice di byte che contiene dati del datagramma.
Eccezioni
asyncResult è null.
asyncResult non è stato restituito da una chiamata al BeginReceive(AsyncCallback, Object) metodo .
EndReceive(IAsyncResult, IPEndPoint) è stato chiamato in precedenza per la lettura asincrona.
Si è verificato un errore durante il tentativo di accesso all'oggetto sottostante Socket.
L'oggetto sottostante Socket è stato chiuso.
Esempio
Nell'esempio di codice seguente viene BeginSend usato per completare una ricezione asincrona di una risposta del server.
public struct UdpState
{
public UdpClient u;
public IPEndPoint e;
}
public static bool messageReceived = false;
public static void ReceiveCallback(IAsyncResult ar)
{
UdpClient u = ((UdpState)(ar.AsyncState)).u;
IPEndPoint e = ((UdpState)(ar.AsyncState)).e;
byte[] receiveBytes = u.EndReceive(ar, ref e);
string receiveString = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine($"Received: {receiveString}");
messageReceived = true;
}
public static void ReceiveMessages()
{
// Receive a message and write it to the console.
IPEndPoint e = new IPEndPoint(IPAddress.Any, s_listenPort);
UdpClient u = new UdpClient(e);
UdpState s = new UdpState();
s.e = e;
s.u = u;
Console.WriteLine("listening for messages");
u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
// Do some work while we wait for a message. For this example, we'll just sleep
while (!messageReceived)
{
Thread.Sleep(100);
}
}
Commenti
Questo metodo si blocca fino al completamento dell'operazione.
Per eseguire questa operazione in modo sincrono, usare il Receive metodo .