UdpClient.EndReceive(IAsyncResult, IPEndPoint) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengakhiri penerimaan asinkron yang tertunda.
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()
Parameter
- asyncResult
- IAsyncResult
Objek IAsyncResult yang dikembalikan oleh panggilan ke BeginReceive(AsyncCallback, Object).
- remoteEP
- IPEndPoint
Titik akhir jarak jauh yang ditentukan.
Mengembalikan
Jika berhasil, array byte yang berisi datagram.
Pengecualian
asyncResult
adalah null
.
asyncResult
tidak dikembalikan oleh panggilan ke BeginReceive(AsyncCallback, Object) metode .
EndReceive(IAsyncResult, IPEndPoint) sebelumnya dipanggil untuk pembacaan asinkron.
Terjadi kesalahan saat mencoba mengakses yang mendasar Socket.
Yang mendasar Socket telah ditutup.
Contoh
Contoh kode berikut menggunakan BeginSend untuk menyelesaikan penerimaan asinkron dari respons server.
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);
}
}
Keterangan
Metode ini memblokir hingga operasi selesai.
Untuk melakukan operasi ini secara sinkron, gunakan Receive metode .