本文整理汇总了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;
}
示例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);
}
}
示例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);
}
}
示例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);
}
}