65,210
社区成员
发帖
与我相关
我的任务
分享 private void Send(byte[] SendBuf)
{
UdpClient objUdpClient = new UdpClient(ClsCommand.IP, int.Parse(ClsCommand.SendPort));
objUdpClient.Send(SendBuf, SendBuf.Length);
}
/// <summary>
/// 启动UDP接收
/// </summary>
private void Start()
{
//初始化窗体时,启动接收数据
if (udpClient != null)
{
UdpThread.Abort();
Thread.Sleep(TimeSpan.FromMilliseconds(500d));
udpClient.Close();
}
try
{
udpClient = new UdpClient(Int32.Parse(ClsCommand.RecevicePort));
UdpThread = new Thread(new ThreadStart(UdpReciveThread));
UdpThread.Start();
}
catch (Exception y)
{
MessageBox.Show(this, y.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 接收数据线程
/// </summary>
private void UdpReciveThread()
{
IPEndPoint remoteHost = null;
while (udpClient != null && Thread.CurrentThread.ThreadState.Equals(System.Threading.ThreadState.Running))
{
Byte[] buf = null; try { buf = udpClient.Receive(ref remoteHost); }
catch { }
if (buf != null)
{
if (buf.Length != 0)
{
//用接收到的字节流去....
ShowForm(buf);
}
}
}
}