110,549
社区成员
发帖
与我相关
我的任务
分享
public class SocketUDP : Component
{
private string m_Description;
private IPEndPoint m_Server;
private Container components;
private Thread thdUdp;
private UdpClient UDP_Server;
private int UDP_Server_Port;
/// <summary>
/// 数据达到事件
/// </summary>
public event DataArrivalEventHandler DataArrival;
private void OnDataArrival(byte[] data, IPAddress ip, int port)
{
if (this.DataArrival != null)
{
System.ComponentModel.ISynchronizeInvoke aSynch = this.DataArrival.Target as System.ComponentModel.ISynchronizeInvoke;
if (aSynch != null && aSynch.InvokeRequired)
aSynch.Invoke(new DataArrivalEventHandler(OnDataArrival), new object[] { data, ip, port });
else
this.DataArrival(data, ip, port);
}
}
/// <summary>
/// 套接字出错事件
/// </summary>
public event SocketErrorEventHandler SocketError;
private void OnSocketError(string error)
{
if (this.SocketError != null)
{
System.ComponentModel.ISynchronizeInvoke aSynch = this.SocketError.Target as System.ComponentModel.ISynchronizeInvoke;
if (aSynch != null && aSynch.InvokeRequired)
aSynch.Invoke(new SocketErrorEventHandler(SocketError), new object[] { error });
else
this.SocketError(error);
}
}
public SocketUDP()
{
this.components = null;
this.m_Server = new IPEndPoint(IPAddress.Any, 0);
this.UDP_Server = new UdpClient();
this.m_Description = "";
this.InitializeComponent();
}
public SocketUDP(IContainer container)
{
this.components = null;
this.m_Server = new IPEndPoint(IPAddress.Any, 0);
this.UDP_Server = new UdpClient();
this.m_Description = "";
container.Add(this);
this.InitializeComponent();
}
/// <summary>
/// 关闭套接字
/// </summary>
public void CloseSocket()
{
Thread.Sleep(30);
try
{
if (this.UDP_Server != null)
this.UDP_Server.Close();
if (this.thdUdp != null)
this.thdUdp.Abort();
}
catch (Exception e)
{
}
}
protected override void Dispose(bool disposing)
{
if (disposing && (this.components != null))
{
this.components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// 接受UDP数据
/// </summary>
private void GetUDPData()
{
while (true)
{
try
{
byte[] rData = this.UDP_Server.Receive(ref this.m_Server);
this.OnDataArrival(rData, this.m_Server.Address, this.m_Server.Port);
Thread.Sleep(0);
}
catch (System.ObjectDisposedException)
{
break;
}
catch (System.Threading.ThreadAbortException)
{
break;
}
catch (Exception e)
{
this.OnSocketError(e.ToString());
}
}
}
private void InitializeComponent()
{
this.components = new Container();
}
/// <summary>
/// 开始侦听指定的端口
/// </summary>
/// <param name="Port"></param>
public void Listen(int Port)
{
try
{
this.UDP_Server_Port = Port;
this.UDP_Server = new UdpClient(Port);
this.thdUdp = new Thread(new ThreadStart(this.GetUDPData));
this.thdUdp.Start();
}
catch (Exception e)
{
this.OnSocketError(e.ToString());
}
}
/// <summary>
/// 发送数据到指定的IP与端口
/// </summary>
/// <param name="Host"></param>
/// <param name="Port"></param>
/// <param name="Data"></param>
public void Send(IPAddress Host, int Port, byte[] Data)
{
try
{
IPEndPoint server = new IPEndPoint(Host, Port);
this.UDP_Server.Send(Data, Data.Length, server);
}
catch (Exception e)
{
this.OnSocketError(e.ToString());
}
}
[Category("UDPSocket"), Description("功能描述"), Browsable(true)]
public string Description
{
get { return this.m_Description; }
set { this.m_Description = value; }
}
[Category("UDPSocket"), Description("获得对方IP地址与端口号等信息."), Browsable(true)]
public IPEndPoint Server
{
get { return this.m_Server; }
set { this.m_Server = value; }
}
public int SocketUDPPort
{
get { return this.UDP_Server_Port; }
}
public delegate void DataArrivalEventHandler(byte[] data, IPAddress ip, int port);
public delegate void SocketErrorEventHandler(string error);
}
if (InvokeRequired)
{
//传递一个委托
}