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
Extremo remoto especificado.
Devoluciones
Si se completa correctamente, una matriz de bytes que contiene los datos del datagrama.
Excepciones
asyncResult
es null
.
asyncResult
no se devolvió mediante una llamada al método BeginReceive(AsyncCallback, Object).
Se llamó previamente a EndReceive(IAsyncResult, IPEndPoint) para la lectura asincrónica.
Ocurrió un error al intentar obtener acceso al Socket subyacente.
El objeto Socket subyacente se ha cerrado.
Ejemplos
En el ejemplo de código siguiente se usa BeginSend para completar una recepción asincrónica de una respuesta del servidor.
private:
static int listenPort = 13000;
public:
value struct UdpState
{
public:
UdpClient^ udpClient;
IPEndPoint^ ipEndPoint;
};
static bool isMessageReceived;
static void ReceiveCallback(IAsyncResult^ asyncResult)
{
UdpClient^ udpClient =
((UdpState)(asyncResult->AsyncState)).udpClient;
IPEndPoint^ ipEndPoint =
((UdpState)(asyncResult->AsyncState)).ipEndPoint;
array<Byte>^ receiveBytes =
udpClient->EndReceive(asyncResult, ipEndPoint);
String^ receiveString =
Encoding::ASCII->GetString(receiveBytes);
Console::WriteLine("Received: {0}", receiveString);
isMessageReceived = true;
}
static void ReceiveMessages()
{
// Receive a message and write it to the console.
IPEndPoint^ ipEndPoint = gcnew IPEndPoint(IPAddress::Any, listenPort);
UdpClient^ udpClient = gcnew UdpClient(ipEndPoint);
UdpState^ udpState = gcnew UdpState();
udpState->ipEndPoint = ipEndPoint;
udpState->udpClient = udpClient;
Console::WriteLine("listening for messages");
udpClient->BeginReceive(gcnew AsyncCallback(ReceiveCallback),
udpState);
// Do some work while we wait for a message. For this example,
// we'll just sleep
while (!isMessageReceived)
{
Thread::Sleep(100);
}
}
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 .