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

UDP客户端与服务器异步通信
本文介绍了一个使用UDP协议实现客户端与服务器之间的异步通信的示例,包括客户端接收和服务器响应信息的过程。

接受端:

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
/*异步接受类,并异步响应*/
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 = "这是服务器响应信息";
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("已经发送的字节数位:" + u.EndSend(result));
}

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


}

发送端:

  

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("第二次发送");
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("正在接收服务器响应信息中.....");
Thread.Sleep(2000);
byte[] recvData = u.EndReceive(result, ref p);
string rData = Encoding.Default.GetString(recvData);
Console.WriteLine("sender接收到的服务器的respMsg" + rData);

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

}

public void SendCallback(IAsyncResult result)
{

UdpClient u = (UdpClient)result.AsyncState;
Console.WriteLine("已经发送的字节数位:" + 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();
}
}
}

代码仅供参考

转载于:https://www.cnblogs.com/biGpython/archive/2012/03/06/2382403.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值