AsyncUdpClient 类

本文介绍了一个异步UDP客户端的实现方法,通过C#语言展示了如何创建、监听及发送UDP消息,并提供了事件处理机制来接收反馈。
ContractedBlock.gifExpandedBlockStart.gifView Code
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace Rocky.Net
{
public class AsyncUdpClient
{
public static Socket ReuseAddress(IPAddress address, int port)
{
Socket Listener
= new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress,
1);
Listener.Bind(
new IPEndPoint(IPAddress.Any, port));
Listener.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
new MulticastOption(address));
return Listener;
}

public event AsyncUdpEventHandler Sent;
public event AsyncUdpEventHandler Received;

private byte[] buffer;
private bool isListening;
private IPEndPoint listenEndPoint;
private Socket sock;

public bool IsListening
{
get { return isListening; }
}
public IPEndPoint ListenEndpoint
{
get { return listenEndPoint; }
}
public Socket Client
{
get { return sock; }
}

public AsyncUdpClient()
{
buffer
= new byte[BufferUtility.DefaultBufferSize];
sock
= new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
public AsyncUdpClient(int port)
{
listenEndPoint
= new IPEndPoint(IPAddress.Any, port);
}

public void StartListen()
{
if (!isListening)
{
if (!sock.IsBound)
{
sock.Bind(listenEndPoint);
}
IPEndPoint epSender
= new IPEndPoint(IPAddress.Any, 0);
EndPoint epRemote
= (EndPoint)epSender;
sock.BeginReceiveFrom(buffer,
0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(ReceiveCallBack), epRemote);
isListening
= true;
}
}

public void StopListen()
{
isListening
= false;
}

public void Send(IPEndPoint epRemote, byte[] data)
{
sock.BeginSendTo(data,
0, data.Length, SocketFlags.None, epRemote, new AsyncCallback(SendCallBack), epRemote);
}

public void Close()
{
sock.Close(
200);
}

private void ReceiveCallBack(IAsyncResult ar)
{
EndPoint epRemote
= (EndPoint)ar.AsyncState;
int recv = sock.EndReceiveFrom(ar, ref epRemote);
if (Received != null)
{
Received(
this, new AsyncUdpEventArgs(epRemote, buffer));
}
if (isListening)
{
sock.BeginReceiveFrom(buffer,
0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(ReceiveCallBack), epRemote);
}
}
private void SendCallBack(IAsyncResult ar)
{
EndPoint epRemote
= (EndPoint)ar.AsyncState;
int sent = sock.EndSend(ar);
if (Sent != null)
{
Sent(
this, new AsyncUdpEventArgs(epRemote, buffer));
}
}
}
}
ContractedBlock.gifExpandedBlockStart.gifView Code
using System;
using System.Net;

namespace Rocky.Net
{
public delegate void AsyncUdpEventHandler(object sender, AsyncUdpEventArgs e);

public class AsyncUdpEventArgs : EventArgs
{
private EndPoint _remoteEndPoint;
private byte[] _data;

public EndPoint RemoteEndPoint
{
get { return _remoteEndPoint; }
}
public byte[] Data
{
get { return _data; }
}

public AsyncUdpEventArgs(EndPoint remoteEndPoint, byte[] data)
{
_remoteEndPoint
= remoteEndPoint;
_data
= data;
}
}
}

转载于:https://www.cnblogs.com/Googler/archive/2011/03/19/1988999.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值