日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > C# >内容正文

C#

.net Csharpt C# UDP 异步发送信息 代码实例

發(fā)布時(shí)間:2025/4/14 C# 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .net Csharpt C# UDP 异步发送信息 代码实例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

接受端:

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
/*異步接受類,并異步響應(yīng)*/
namespace HostBackupReciever
{
class UpdData
{
public UdpClient u = null;
public IPEndPoint p = null;
}

public class Receive2
{
private bool msgGet = false;

public void Receive()
{
IPEndPoint p = new IPEndPoint(IPAddress.Any,11000);
UdpClient u = new UdpClient(p);
u.BeginReceive(new AsyncCallback(ReceiveCallback),u);
}

public void ReceiveCallback(IAsyncResult result)
{
UdpClient u = (UdpClient)result.AsyncState;
IPEndPoint p = new IPEndPoint(IPAddress.Any, 11000);
Console.WriteLine("正在接收中.....");
Thread.Sleep(6000);
byte[] recvData = u.EndReceive(result, ref p);
string rData = Encoding.Default.GetString(recvData);
Console.WriteLine(rData);
msgGet = true;
if (msgGet)
{
string respMsg = "這是服務(wù)器響應(yīng)信息";
byte[] respDate = Encoding.Default.GetBytes(respMsg);

u.BeginSend(respDate, respDate.Length, p,new AsyncCallback(SendCallback), u);
}
u.BeginReceive(new AsyncCallback(ReceiveCallback), u);

}

public void SendCallback(IAsyncResult result)
{
UdpClient u = (UdpClient)result.AsyncState;
Console.WriteLine("已經(jīng)發(fā)送的字節(jié)數(shù)位:" + u.EndSend(result));
}

public static void Main(string[] args)
{
Receive2 r2 = new Receive2();
r2.Receive();
Console.ReadKey();
}
}


}

發(fā)送端:

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketPros
{
class Sender2
{
public void Send(string server,string msg)
{
UdpClient u = new UdpClient(12000);
IPEndPoint p = new IPEndPoint(IPAddress.Parse(server), 11000);

byte[] sendData = Encoding.Default.GetBytes(msg);
u.BeginSend(sendData, sendData.Length,p, new AsyncCallback(SendCallback), u);

byte[] sendData2 = Encoding.Default.GetBytes("第二次發(fā)送");
u.BeginSend(sendData2, sendData2.Length, p, new AsyncCallback(SendCallback), u);

u.BeginReceive(new AsyncCallback(ReceiveCallback), u);
}

public void ReceiveCallback(IAsyncResult result)
{
UdpClient u = (UdpClient)result.AsyncState;
IPEndPoint p = new IPEndPoint(IPAddress.Any, 11000);
Console.WriteLine("正在接收服務(wù)器響應(yīng)信息中.....");
Thread.Sleep(2000);
byte[] recvData = u.EndReceive(result, ref p);
string rData = Encoding.Default.GetString(recvData);
Console.WriteLine("sender接收到的服務(wù)器的respMsg" + rData);

u.BeginReceive(new AsyncCallback(ReceiveCallback), u);

}

public void SendCallback(IAsyncResult result)
{

UdpClient u = (UdpClient)result.AsyncState;
Console.WriteLine("已經(jīng)發(fā)送的字節(jié)數(shù)位:" + u.EndSend(result));
//msgSent = true;
}

public static void Main(string[] args)
{
Sender2 s2 = new Sender2();
s2.Send("127.0.0.1","what a fucking code!!!!");
Console.ReadKey();
}
}
}

代碼僅供參考

轉(zhuǎn)載于:https://www.cnblogs.com/biGpython/archive/2012/03/06/2382403.html

總結(jié)

以上是生活随笔為你收集整理的.net Csharpt C# UDP 异步发送信息 代码实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。