當前位置: 首頁>>代碼示例>>C#>>正文


C# UdpClient.BeginSend方法代碼示例

本文整理匯總了C#中System.Net.Sockets.UdpClient.BeginSend方法的典型用法代碼示例。如果您正苦於以下問題:C# UdpClient.BeginSend方法的具體用法?C# UdpClient.BeginSend怎麽用?C# UdpClient.BeginSend使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Net.Sockets.UdpClient的用法示例。


在下文中一共展示了UdpClient.BeginSend方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: SendCallback

public static bool messageSent = false;

public static void SendCallback(IAsyncResult ar)
{
    UdpClient u = (UdpClient)ar.AsyncState;

    Console.WriteLine($"number of bytes sent: {u.EndSend(ar)}");
    messageSent = true;
}
開發者ID:.NET開發者,項目名稱:System.Net.Sockets,代碼行數:9,代碼來源:UdpClient.BeginSend

示例2: SendMessage3

static void SendMessage3(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();

    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // send the message
    // the destination is defined by the server name and port
    u.BeginSend(sendBytes, sendBytes.Length, server, s_listenPort, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}
開發者ID:.NET開發者,項目名稱:System.Net.Sockets,代碼行數:17,代碼來源:UdpClient.BeginSend

示例3: SendMessage1

static void SendMessage1(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();

    u.Connect(server, s_listenPort);
    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // send the message
    // the destination is defined by the call to .Connect()
    u.BeginSend(sendBytes, sendBytes.Length, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}
開發者ID:.NET開發者,項目名稱:System.Net.Sockets,代碼行數:18,代碼來源:UdpClient.BeginSend

示例4: SendMessage2

static void SendMessage2(string server, string message)
{
    // create the udp socket
    UdpClient u = new UdpClient();
    byte[] sendBytes = Encoding.ASCII.GetBytes(message);

    // resolve the server name
    IPHostEntry heserver = Dns.GetHostEntry(server);

    IPEndPoint e = new IPEndPoint(heserver.AddressList[0], s_listenPort);

    // send the message
    // the destination is defined by the IPEndPoint
    u.BeginSend(sendBytes, sendBytes.Length, e, new AsyncCallback(SendCallback), u);

    // Do some work while we wait for the send to complete. For this example, we'll just sleep
    while (!messageSent)
    {
        Thread.Sleep(100);
    }
}
開發者ID:.NET開發者,項目名稱:System.Net.Sockets,代碼行數:21,代碼來源:UdpClient.BeginSend


注:本文中的System.Net.Sockets.UdpClient.BeginSend方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。