请看下面的代码错误在化线的部分using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
using System.Threading;namespace QQclass
{
public partial class UDPSocket:Component
{
private IPEndPoint ServerEndPoint = null;
private UdpClient UDP_Server = new UdpClient();
private System.Threading.Thread thdUdp; public delegate void DataArrivalEventHandler(byte[] Data, IPAddress Ip, int Port);
public event DataArrivalEventHandler DataArrival;
private string localHost = "127.0.0.1";
[Browsable(true), Category("Local"), Description("本地IP地址")] public string LocalHost
{
get {return localHost; }
set { localHost = value;}
} private int localPort = 11000;
[Browsable(true), Category("Local"), Description("本地端口号")] public int LocalPort
{
get { return localPort; }
set { localPort = value; }
} private bool active = false;
[Browsable(true), Category("Local"), Description("激活监听")] public bool Active
{
get { return active; }
set { active = value;
if (active)
{ OpenSocket(); } else
{
CloseSocket() ;
} }
} public UDPSocket()
{
// InitializeComponent(); } public UDPSocket(IContainer container)
{
container.Add(this);
// InitializeComponent();
} private void OpenSocket()
{
Listener(); }
private void CloseSocket()
{
if (UDP_Server != null)
UDP_Server.Close();
if (thdUdp != null)
{
Thread.Sleep(30);
thdUdp.Abort();
}
}
protected void Listener()
{
ServerEndPoint = new IPEndPoint(IPAddress.Any ,localPort);
if (UDP_Server != null)
UDP_Server.Close();
UDP_Server = new UdpClient(localPort); try
{
thdUdp = new Thread(new ThreadStart(GetUDPDate));
thdUdp.Start(); } catch (Exception e)
{
MessageBox.Show(e.ToString());
} }
private void GetUDPDate()
{
while (active)
{ try
{ byte[] Data = UDP_Server.Receive(ref ServerEndPoint);
if (DataArrival != null)
{
DataArrival(Data, ServerEndPoint.Address, ServerEndPoint.Port);
} Thread.Sleep(0);
} catch { }
} }
public void Send(System.Net.IPAddress Host, int Port, byte[] Data)
{ try
{
IPEndPoint server = new IPEndPoint(Host, Port);
UDP_Server.Send(Data, Data.Length, server); }
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}
解决方案 »
- 再发一贴,关于绘图
- 急!用正则表达式 匹配一串字符并取出字段
- 求助 怎么把GridView中的数据导入一个DataTable或者DataSet中
- 水晶报表在客户机上有异常,本地正常,求高手帮忙?
- 麻烦大家绑我把这段画excel 饼图的代码翻译成 c# 谢谢大家了!
- asp.net如何用Timer控件处理按钮事件?
- 奋战一夜研究传说中百度的试题,用C#做二进制运算得到2.5亿数字中不重复数字数的O(n)算法,庆祝一下!同时发现了.NET一个BUG,BitArray构造函数的整数是有上限的,达不到Int.Maxvalue.
- 复制
- 求一份【手机(短信猫)收发短信】的源码
- 获得数据库纪录的条数????
- c#bloom filter源代码
- [求助] 页面跳转aspxerrorpath错误
可能是如果要在线程中更新界面控件要是使用 if (InvokeRequired)
{
//传递一个委托
}
{
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);
}