文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>AsyncUdpClient 类

AsyncUdpClient 类

时间:2011-03-19  来源:高级打字员

View 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));
}
}
}
}
View 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;
}
}
}
相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载