UdpClient.BeginReceive メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > UdpClient.BeginReceive メソッドの意味・解説 

UdpClient.BeginReceive メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

データグラムリモート ホストから非同期的に受信します

名前空間: System.Net.Sockets
アセンブリ: System (system.dll 内)
構文構文

Public Function BeginReceive ( _
    requestCallback As AsyncCallback, _
    state As Object _
) As IAsyncResult
Dim instance As UdpClient
Dim requestCallback As AsyncCallback
Dim state As Object
Dim returnValue As IAsyncResult

returnValue = instance.BeginReceive(requestCallback, state)
public IAsyncResult BeginReceive (
    AsyncCallback requestCallback,
    Object state
)
public:
IAsyncResult^ BeginReceive (
    AsyncCallback^ requestCallback, 
    Object^ state
)
public IAsyncResult BeginReceive (
    AsyncCallback requestCallback, 
    Object state
)
public function BeginReceive (
    requestCallback : AsyncCallback, 
    state : Object
) : IAsyncResult

パラメータ

requestCallback

操作完了時に呼び出すメソッド参照する AsyncCallback デリゲート

state

受信操作に関する情報格納するユーザー定義のオブジェクト。このオブジェクトは、操作完了時に requestCallback デリゲート渡されます。

戻り値
非同期受信参照する IAsyncResult オブジェクト

解説解説
使用例使用例

BeginReceive使用してサーバーからの応答非同期的に受信するコード例次に示します

public static bool messageReceived
 = false;

public static void ReceiveCallback(IAsyncResult
 ar)
{
  UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
  IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;

  Byte[] receiveBytes = u.EndReceive(ar, ref e);
  string receiveString = Encoding.ASCII.GetString(receiveBytes);

  Console.WriteLine("Received: {0}", receiveString);
  messageReceived = true;
}

public static void ReceiveMessages()
{
  // Receive a message and write it to the console.
  IPEndPoint e = new IPEndPoint(IPAddress.Any, 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);
  }
}
public:
    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);
        }
    }
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
UdpClient クラス
UdpClient メンバ
System.Net.Sockets 名前空間



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「UdpClient.BeginReceive メソッド」の関連用語

UdpClient.BeginReceive メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



UdpClient.BeginReceive メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS