Common.UdpLib
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
namespace Common.UdpLib
{
public class _Bgz_UdpState
{
public IPEndPoint _UdpClientAddress = new IPEndPoint(IPAddress.Any, 0);
public byte[] _Buffer = new byte[0];
public _Bgz_UdpState()
{
_UdpClientAddress = new IPEndPoint(IPAddress.Any, 0);
_Buffer = new byte[0];
}
public string StrUpdClientAddress
{
get
{
return _UdpClientAddress.Address.ToString() + ":" + _UdpClientAddress.Port.ToString();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Common.UdpLib
{
public enum ErrorType { MaxConnect, Catch, DisConnect, DisAccept };
//Udp
public delegate void _Bgz_OnUdpBindEventDelegate(_Bgz_UdpState state);
public delegate void _Bgz_OnUdpErrorEventDelegate(ErrorType errortype, string errormsg, _Bgz_UdpState state);
public delegate void _Bgz_OnUdpReceiveEventDelegate(_Bgz_UdpState state);
}
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
namespace Common.UdpLib
{
public class UdpSocket
{
private UdpClient _listener = new UdpClient();
private int _port= 0;
private bool _debug;
private Thread _thread;
private struct ClientInfo
{
public string IpAddress;
public int Port;
public ClientInfo(string IpAddress, int Port)
{
this.IpAddress = IpAddress;
this.Port = Port;
}
public ClientInfo(IPEndPoint ip)
{
this.IpAddress = ip.Address.ToString();
this.Port = ip.Port;
}
public string ClientID
{
get
{
return IpAddress + ":" + Port.ToString();
}
}
public IPEndPoint GetIPEndPoint(string IpAddress, int Port)
{
try
{
IPAddress ip = Dns.GetHostEntry(IpAddress).AddressList[0];
return new IPEndPoint(ip, Port);
}
catch
{
return new IPEndPoint(IPAddress.Parse("127.0.0.1"), 0);
}
}
public IPEndPoint IPEndPoint
{
get
{
return GetIPEndPoint(this.IpAddress, this.Port);
}
}
}
private List<ClientInfo> _clientInfo;
#region define delegates
/// <summary>
/// 服务器绑定后处理事件
/// </summary>
public _Bgz_OnUdpBindEventDelegate FOnBindEventDelegate;
/// <summary>
/// 接收到客户端数据后处理事件
/// </summary>
public _Bgz_OnUdpReceiveEventDelegate FOnReceiveEventDelegate;
/// <summary>
/// 报错信息处理
/// </summary>
public _Bgz_OnUdpErrorEventDelegate FOnErrorEventDelegate;
#endregion
#region Event
private void OnBindEvent(_Bgz_UdpState state)
{
if (FOnBindEventDelegate != null) FOnBindEventDelegate(state);
}
private void OnReceiveEvent(_Bgz_UdpState state)
{
if (FOnReceiveEventDelegate != null) FOnReceiveEventDelegate(state);
}
private void OnErrorEvent(ErrorType errortype, string msg, _Bgz_UdpState state)
{
if (FOnErrorEventDelegate != null) FOnErrorEventDelegate(errortype, msg, state);
}
#endregion
#region Constructor and Destructor
public UdpSocket()
{
}
public UdpSocket(int port)
{
_port = port;
}
~UdpSocket()
{
Stop();
}
#endregion
#region Private Methods
private void Receive()
{
try
{
_Bgz_UdpState stx = new _Bgz_UdpState();
while (true)
{
stx._Buffer = new byte[0];
stx._UdpClientAddress = new IPEndPoint(IPAddress.Any, _port);
stx._Buffer = _listener.Receive(ref stx._UdpClientAddress);
ClientInfo ci = new ClientInfo(stx._UdpClientAddress.Address.ToString(), _port);
if (!_clientInfo.Contains(ci))
{
_clientInfo.Add(ci);
}
OnReceiveEvent(stx);
}
}
catch (Exception ex)
{
if (Debug)
{
OnErrorEvent(ErrorType.Catch, "ReceiveCallback.2 Error![Message]:\r\n" + ex.Message + "[StackTrace]:\r\n" + ex.StackTrace + "\r\n", null);
}
}
}
#endregion
#region Public Methods
public void Start()
{
try
{
_listener = new UdpClient(_port);
_Bgz_UdpState stx = new _Bgz_UdpState();
stx._Buffer = new byte[0];
stx._UdpClientAddress = new IPEndPoint(IPAddress.Any, _port);
OnBindEvent(stx);
_clientInfo = new List<ClientInfo>(100);
_thread = new Thread(new ThreadStart(Receive));
_thread.Name = "UdpServer Listener";
_thread.Start();
}
catch (Exception ex)
{
if (Debug)
{
OnErrorEvent(ErrorType.Catch, "Start Error![Message]:\r\n" + ex.Message + "[StackTrace]:\r\n" + ex.StackTrace + "\r\n", null);
}
}
}
public void Stop()
{
try
{
_clientInfo.Clear();
this._listener.Close();
if (this._thread.IsAlive)
{
this._thread.Abort();
}
}
catch (Exception ex)
{
if (Debug)
{
OnErrorEvent(ErrorType.Catch, "Send Error![Message]:\r\n" + ex.Message + "[StackTrace]:\r\n" + ex.StackTrace + "\r\n", null);
}
}
}
public void Send(string hostname, int port, byte[] msg)
{
try
{
IPAddress ip = Dns.GetHostEntry(hostname).AddressList[0];
_listener.Send(msg, msg.Length, new IPEndPoint(ip, port));
}
catch (Exception ex)
{
if (Debug)
{
OnErrorEvent(ErrorType.Catch, "Send Error![Message]:\r\n" + ex.Message + "[StackTrace]:\r\n" + ex.StackTrace + "\r\n", null);
}
}
}
public void Send(IPEndPoint ip, byte[] msg)
{
try
{
_listener.Send(msg, msg.Length, ip);
}
catch (Exception ex)
{
if (Debug)
{
OnErrorEvent(ErrorType.Catch, "Send Error![Message]:\r\n" + ex.Message + "[StackTrace]:\r\n" + ex.StackTrace + "\r\n", null);
}
}
}
public void Send(byte[] msg)
{
foreach(ClientInfo c in _clientInfo)
{
Send(c.IPEndPoint, msg);
}
}
#endregion
#region property
public bool Debug
{
get
{
return _debug;
}
set
{
_debug = value;
}
}
#endregion
}
}