次の方法で共有


UdpClient.EndReceive(IAsyncResult, IPEndPoint) メソッド

定義

保留中の非同期受信を終了します。

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()

パラメーター

asyncResult
IAsyncResult

BeginReceive(AsyncCallback, Object) の呼び出しによって返される IAsyncResult オブジェクト。

remoteEP
IPEndPoint

指定したリモート エンドポイント。

戻り値

Byte[]

成功した場合は、データグラム データを格納するバイトの配列。

例外

asyncResultnullです。

BeginReceive(AsyncCallback, Object) メソッドへの呼び出しで asyncResult が返されませんでした。

EndReceive(IAsyncResult, IPEndPoint) が、非同期の読み取りのために以前に呼び出されています。

基になる Socket へのアクセスを試みているときにエラーが発生しました。

基になる Socket は閉じられています。

次のコード例では、 を使用 BeginSend して、サーバー応答の非同期受信を完了します。

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

注釈

このメソッドは、操作が完了するまでブロックします。

この操作を同期的に実行するには、 メソッドを使用します Receive

適用対象