前些日子,做了一个实时采集数据的软件,并要求实时通过网络发送出去,采用UDP方式,也需要接收命令等信息。
因此做了下面一个UDPClient发送和接收数据的类完成此功能。该类在发送时使用RemoteHostName ,SendPort定义远程接收主机的地址和端口。代码中对几个地方进行了说明,主要是实际调试中发现的问题以及注意的地方。代码已经调试通过,希望对大家有些提示。
由于本人的能力有限,可能代码中存在缺陷,希望大家指正。
using
System;
using System.Text ;
using System.Net;
using System.Net.Sockets;
using System.Threading ;
namespace msgserver
... {
/**//// <summary>
/// netmsg 的摘要说明。
/// </summary>
public class netmsg:System.IDisposable
...{
private System.Net.Sockets.UdpClient UCS ;
public Thread Thread1;
private bool _Stop=false;
public delegate void RemoteCmd(string e);
public event RemoteCmd ReceiveRemoteCmd;
public string RemoteHostName="";
public int SendPort=2000;
public netmsg()
...{
UCS=new System.Net.Sockets.UdpClient();
}
/**//// <summary>
/// 类型的析构,IDisposable接口
/// </summary>
public void Dispose()
...{
_Stop=true;
UCS=null;
GC.Collect();
}
public void StartReceive()
...{
if(Thread1==null)
...{
_Stop=false;
Thread1=new Thread(new ThreadStart(ReceiveThread));
Thread1.Name="ReceiveThread";
Thread1.Priority=ThreadPriority.Normal;
Thread1.IsBackground=true; //一定要加上,否则接收不到数据,
Thread1.Start();
}
}
public void StopReceive()
...{
try
...{
string s=System.Net.Dns.GetHostName().Trim();
bool r=false;;
_Stop=true;
if(s.Length>0)
...{
r=SendCmd(s,"STOP");
}
if((!r)||s.Length==0)
...{
if((Thread1.ThreadState & ThreadState.Running )==ThreadState.Running)
...{
Thread1.Abort();
}
if((Thread1.ThreadState & ThreadState.AbortRequested )==ThreadState.AbortRequested)
...{
GC.Collect();
}
if((Thread1.ThreadState & ThreadState.Aborted )==ThreadState.Aborted
||(Thread1.ThreadState & ThreadState.Stopped )==ThreadState.Stopped)
...{
Thread1=null;
_IsReceiving=false;
if(ReceiveThreadStoped!=null)
ReceiveThreadStoped();
}
}
}
catch
...{
}
}
private void ReceiveThread()
...{
UdpClient UCR;
//new UdpClient(RemoteHostName,SendPort);
//new UdpClient();
//上面两个都不行,接收不到数据。
try
...{
UCR=new UdpClient(SendPort);
//IPEndPoint Sender = new IPEndPoint(IPAddress.Any ,0);
//取0可接收其它端口的数据,这里上面new UdpClient(SendPort)的设计好象没有起到应有的作用。
IPEndPoint Sender = new IPEndPoint(IPAddress.Any ,SendPort);
while(!_Stop)
...{
try
...{
//Receive处于等待,但不影响发送
byte[] Data = UCR.Receive ( ref Sender);
string returnData = Encoding.Default.GetString(Data);
if (ReceiveRemoteCmd!=null)
ReceiveRemoteCmd(returnData);
}
catch (Exception s)
...{
Console.WriteLine(s.ToString());
}
Thread.Sleep(1000);
}
}
catch
...{}
UCR.Close();
UCR=null;
}
public bool SendCmd(string RemoteHostName,string cmd)
...{
bool r=false;
if(RemoteHostName.Length>0)
...{
try
...{
Byte[] sendBytes2 = Encoding.Default.GetBytes(cmd);
UC.Send(sendBytes2, sendBytes2.Length,RemoteHostName ,AppCfg.LocalHostPort);
r=true;
}
catch(Exception s)
...{
if(ErrEvent!=null)
ErrEvent(s.Message);
}
}
else
...{
if(ErrEvent!=null)
ErrEvent("远程主机没有定义,发送失败!");
}
return r;
}
public void SendCmd(string cmd)
...{
SendCmd(RemoteHostName,cmd);
}
}
}
using System.Text ;
using System.Net;
using System.Net.Sockets;
using System.Threading ;
namespace msgserver
... {
/**//// <summary>
/// netmsg 的摘要说明。
/// </summary>
public class netmsg:System.IDisposable
...{
private System.Net.Sockets.UdpClient UCS ;
public Thread Thread1;
private bool _Stop=false;
public delegate void RemoteCmd(string e);
public event RemoteCmd ReceiveRemoteCmd;
public string RemoteHostName="";
public int SendPort=2000;
public netmsg()
...{
UCS=new System.Net.Sockets.UdpClient();
}
/**//// <summary>
/// 类型的析构,IDisposable接口
/// </summary>
public void Dispose()
...{
_Stop=true;
UCS=null;
GC.Collect();
}
public void StartReceive()
...{
if(Thread1==null)
...{
_Stop=false;
Thread1=new Thread(new ThreadStart(ReceiveThread));
Thread1.Name="ReceiveThread";
Thread1.Priority=ThreadPriority.Normal;
Thread1.IsBackground=true; //一定要加上,否则接收不到数据,
Thread1.Start();
}
}
public void StopReceive()
...{
try
...{
string s=System.Net.Dns.GetHostName().Trim();
bool r=false;;
_Stop=true;
if(s.Length>0)
...{
r=SendCmd(s,"STOP");
}
if((!r)||s.Length==0)
...{
if((Thread1.ThreadState & ThreadState.Running )==ThreadState.Running)
...{
Thread1.Abort();
}
if((Thread1.ThreadState & ThreadState.AbortRequested )==ThreadState.AbortRequested)
...{
GC.Collect();
}
if((Thread1.ThreadState & ThreadState.Aborted )==ThreadState.Aborted
||(Thread1.ThreadState & ThreadState.Stopped )==ThreadState.Stopped)
...{
Thread1=null;
_IsReceiving=false;
if(ReceiveThreadStoped!=null)
ReceiveThreadStoped();
}
}
}
catch
...{
}
}
private void ReceiveThread()
...{
UdpClient UCR;
//new UdpClient(RemoteHostName,SendPort);
//new UdpClient();
//上面两个都不行,接收不到数据。
try
...{
UCR=new UdpClient(SendPort);
//IPEndPoint Sender = new IPEndPoint(IPAddress.Any ,0);
//取0可接收其它端口的数据,这里上面new UdpClient(SendPort)的设计好象没有起到应有的作用。
IPEndPoint Sender = new IPEndPoint(IPAddress.Any ,SendPort);
while(!_Stop)
...{
try
...{
//Receive处于等待,但不影响发送
byte[] Data = UCR.Receive ( ref Sender);
string returnData = Encoding.Default.GetString(Data);
if (ReceiveRemoteCmd!=null)
ReceiveRemoteCmd(returnData);
}
catch (Exception s)
...{
Console.WriteLine(s.ToString());
}
Thread.Sleep(1000);
}
}
catch
...{}
UCR.Close();
UCR=null;
}
public bool SendCmd(string RemoteHostName,string cmd)
...{
bool r=false;
if(RemoteHostName.Length>0)
...{
try
...{
Byte[] sendBytes2 = Encoding.Default.GetBytes(cmd);
UC.Send(sendBytes2, sendBytes2.Length,RemoteHostName ,AppCfg.LocalHostPort);
r=true;
}
catch(Exception s)
...{
if(ErrEvent!=null)
ErrEvent(s.Message);
}
}
else
...{
if(ErrEvent!=null)
ErrEvent("远程主机没有定义,发送失败!");
}
return r;
}
public void SendCmd(string cmd)
...{
SendCmd(RemoteHostName,cmd);
}
}
}