由于项目需要,首次接触到网络编程,也有缘碰到了UDP;了解了UDP通讯的基本概念和编程知识后,结合网上的部分事例便着手设计UDP通讯程序;测试通过后欣喜若狂,便交给用户试运行,没想到结果却令我大失所望,客户端程序很不稳定;仔细分析后发现是UDP通讯问题,由于客户端是周期性地向服务器请求数据,若客户端的请求指令或是服务端发来的数据包丢失的话,那么客户端将一直等待,陷入假死状态。
所以问题在于要设定客户端接收的超时时间,若服务器端无相应,客户端应该抛出异常,而不是毫无意义的等待。
在网上查阅了大量资料,也发了不少帖子,但始终还是没有解决问题!
直到今天,我按照以前同样的方法,仔细作了一遍,终于大功告成,UDP接收过程设定的超时时间生效!但以前失败的原因还是不得而知,为使以后少走弯路,特将成功后的代码贴出:
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using YD.ZYCAlarm.Buffer;
namespace YD.ZYCAlarm.UDPCommunicate
{
/// <summary>
/// SendCommand 的摘要说明。
/// </summary>
public class SendCommand
{
/// <summary>
/// 要发送的命令文本
/// </summary>
private string m_CmdText;
/// <summary>
///
/// </summary>
private static readonly int BUF_SIZE=0x10000;
/// <summary>
/// 本地UDP对象
/// </summary>
private Socket m_Client ;
/// <summary>
/// 本地端口(也可以不设)
/// </summary>
private int m_LocalPort;
/// <summary>
/// 远程端口
/// </summary>
private int m_RemotePort ;
/// <summary>
/// 远程IP
/// </summary>
private string m_RemoteIP ;
/// <summary>
/// 对方网络节点
/// </summary>
private IPEndPoint m_RemoteEP ;
public SendCommand( string strIP,int iPort,string strCmd )
{
this.m_RemoteIP = strIP ;
this.m_RemotePort = iPort;
this.m_CmdText = strCmd;
m_RemoteEP = new IPEndPoint(IPAddress.Parse( m_RemoteIP ) , m_RemotePort) ;
}
private void ConnectToServer()
{
m_Client=new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp
);
m_Client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout,
3000
);
}
public bool Send()
{
ConnectToServer();
try
{
Byte[] sendBytes = Encoding.ASCII.GetBytes(this.m_CmdText);
this.m_Client.SendTo(sendBytes ,sendBytes.Length,SocketFlags.None,m_RemoteEP) ;
//接收服务端的回应的成功信息
return Receive();
}
catch
{
return false ;
}
}
private bool Receive()
{
byte[] buf=new byte[BUF_SIZE];
EndPoint remoteEndPoint=new IPEndPoint(IPAddress.Any,0);
try
{
int receive = this.m_Client.ReceiveFrom(buf,ref remoteEndPoint) ;
string strData= Encoding.ASCII.GetString(buf,0,receive);
if(strData == "OK")
{
return true ;
}
else
{
return false ;
}
}
catch
{
return false ;
}
finally
{
this.m_Client.Close();
this.m_Client = null ;
}
}
public void Dispose()
{
if(this.m_Client != null)
{
this.m_Client.Close();
this.m_Client = null;
}
}
}
}
using System.Text;
using System.Net;
using System.Net.Sockets;
using YD.ZYCAlarm.Buffer;
namespace YD.ZYCAlarm.UDPCommunicate
{
/// <summary>
/// SendCommand 的摘要说明。
/// </summary>
public class SendCommand
{
/// <summary>
/// 要发送的命令文本
/// </summary>
private string m_CmdText;
/// <summary>
///
/// </summary>
private static readonly int BUF_SIZE=0x10000;
/// <summary>
/// 本地UDP对象
/// </summary>
private Socket m_Client ;
/// <summary>
/// 本地端口(也可以不设)
/// </summary>
private int m_LocalPort;
/// <summary>
/// 远程端口
/// </summary>
private int m_RemotePort ;
/// <summary>
/// 远程IP
/// </summary>
private string m_RemoteIP ;
/// <summary>
/// 对方网络节点
/// </summary>
private IPEndPoint m_RemoteEP ;
public SendCommand( string strIP,int iPort,string strCmd )
{
this.m_RemoteIP = strIP ;
this.m_RemotePort = iPort;
this.m_CmdText = strCmd;
m_RemoteEP = new IPEndPoint(IPAddress.Parse( m_RemoteIP ) , m_RemotePort) ;
}
private void ConnectToServer()
{
m_Client=new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp
);
m_Client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout,
3000
);
}
public bool Send()
{
ConnectToServer();
try
{
Byte[] sendBytes = Encoding.ASCII.GetBytes(this.m_CmdText);
this.m_Client.SendTo(sendBytes ,sendBytes.Length,SocketFlags.None,m_RemoteEP) ;
//接收服务端的回应的成功信息
return Receive();
}
catch
{
return false ;
}
}
private bool Receive()
{
byte[] buf=new byte[BUF_SIZE];
EndPoint remoteEndPoint=new IPEndPoint(IPAddress.Any,0);
try
{
int receive = this.m_Client.ReceiveFrom(buf,ref remoteEndPoint) ;
string strData= Encoding.ASCII.GetString(buf,0,receive);
if(strData == "OK")
{
return true ;
}
else
{
return false ;
}
}
catch
{
return false ;
}
finally
{
this.m_Client.Close();
this.m_Client = null ;
}
}
public void Dispose()
{
if(this.m_Client != null)
{
this.m_Client.Close();
this.m_Client = null;
}
}
}
}