UdpClient.EndReceive(IAsyncResult, IPEndPoint) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Finaliza una recepción asincrónica pendiente.
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()
Parámetros
- asyncResult
- IAsyncResult
Objeto IAsyncResult devuelto por una llamada a BeginReceive(AsyncCallback, Object).
- remoteEP
- IPEndPoint
Punto de conexión remoto especificado.
Devoluciones
Si se ejecuta correctamente, una matriz de bytes que contiene datos de datagramas.
Excepciones
asyncResult es null.
asyncResult no se devolvió mediante una llamada al BeginReceive(AsyncCallback, Object) método .
EndReceive(IAsyncResult, IPEndPoint) anteriormente se llamó a para la lectura asincrónica.
Error al intentar acceder al subyacente Socket.
Se ha cerrado el subyacente Socket .
Ejemplos
En el ejemplo de código siguiente se usa BeginSend para completar una recepción asincrónica de una respuesta del servidor.
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);
}
}
Comentarios
Este método se bloquea hasta que se complete la operación.
Para realizar esta operación de forma sincrónica, use el Receive método .