AsyncUdpClient 类
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));
}
}
}
}
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;
}
}
}
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;
}
}
}