异步Socket Server端长时间运行后调用Send方法成功但Telnet无法收到数据

Essence_zhx 2006-09-22 01:25:53
兄弟最近使用异步Socket开发了一个Server端程序,该Server以大约每秒4到5次向所有的Client端发送固定长度的字符串,字符串长度小于1024。

在程序刚开始的时候运行一切正常,使用telent登录的Server能够正常显示Server发送的消息,但是运行一定时间1天或则2天而且时间长度不一定以后,在使用telent查看该Server时就不能显示数据了,而且Server端的Socket的Send方法不会抛出异常

请教各位大虾应该如何解决:
...全文
370 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
bpmfwu 2007-04-06
  • 打赏
  • 举报
回复
补充一下

一旦程序没有响应,超过1分钟,程序返回false,这个时候要将Socket Close,然后重新开一个TELNET进程,这样,就可以一直顺畅运行下去
bpmfwu 2007-04-06
  • 打赏
  • 举报
回复
我也在写自动TELNET方面的,很多设备因为各种各样的问题(网络/缓冲区大小/设备反应速度),无法及时回应TELNET请求,在程序上表现为假死等状态。

如果你对你的网络环境没有信心的话,建议你在IsInfoEnabled事件里加上判断,我是这样写的,供你参考

public bool WaitFor(string DataToWaitFor) //DataToWaitFor 接收判断字段
{

// Get the starting time
DateTime timeScale = DateTime.Now.AddMinutes(1);
while (strWorkingData.IndexOf(DataToWaitFor) == -1)
{
/*

if(strWorkingData.IndexOf("fail") >0)
{
//throw new Exception("Failed ");
strWorkingData = "";
return false;
}
*/
// Timeout logic
if (DateTime.Compare(timeScale , DateTime.Now )<0)
{
//throw new Exception("Timed Out waiting for : " + DataToWaitFor);
strWorkingData = "";
return false;
}

Thread.Sleep(1000);
}
//Thread.Sleep(000);
strWorkingData = "";
return true;
}
jxf654 2007-02-10
  • 打赏
  • 举报
回复
up
jetxia 2007-02-09
  • 打赏
  • 举报
回复
标记
Red_angelX 2007-02-09
  • 打赏
  • 举报
回复
1推测你代码出问题的地方是Send还是Receive
2仔细检查你代码里面的东西,写上详细的异常谱捉

heilong05 2007-02-09
  • 打赏
  • 举报
回复
Mark
Knight94 2006-09-22
  • 打赏
  • 举报
回复
用系统的监视工具,查看一下server端程序的运行情况,同时自己写日志,看看是否出现什么异常。
Essence_zhx 2006-09-22
  • 打赏
  • 举报
回复
to:
jiezhi(风满袖)

帮忙看看代码有什么不正确的地方。
Essence_zhx 2006-09-22
  • 打赏
  • 举报
回复
private void RefrenshUI()
{
this.listBox1.Items.Clear();

foreach (object key in accpSockets.Keys)
{
Socket s = accpSockets[key] as Socket;

this.listBox1.Items.Add("IP:" + s.RemoteEndPoint.ToString());
}

}

private void ReceiveCallBack(IAsyncResult ar)
{

Socket s = (Socket)ar.AsyncState;
if (s.Connected)
{
try
{
int datacount = ((Socket)ar.AsyncState).EndReceive(ar);
if (datacount > 0)
{
if (log.IsInfoEnabled)
{
log.Info(s.GetHashCode().ToString() + " " + s.RemoteEndPoint.ToString() + " 接受数据 长度:" + datacount.ToString());
}

System.Text.Encoding ender = System.Text.Encoding.UTF8;
string msg = ender.GetString(receiveData, 0, datacount);

if (log.IsInfoEnabled)
{
log.Info(s.GetHashCode().ToString() + " " + s.RemoteEndPoint.ToString() + " 接受内容:" + msg);
}
s.BeginReceive(receiveData, 0, 1024, SocketFlags.None, new AsyncCallback(this.ReceiveCallBack), s);
}
else
{
this.DelAccpSocket(s);
if (log.IsInfoEnabled)
{
log.Info(s.GetHashCode().ToString() + " " + s.RemoteEndPoint.ToString() + " 离线");
}
s.Shutdown(SocketShutdown.Both);
s.Close();

}
}
catch (Exception ex)
{
if (log.IsInfoEnabled)
{
log.Info(ex.ToString());
}
this.DelAccpSocket(s);
if (log.IsInfoEnabled)
{
log.Info(s.GetHashCode().ToString() + " " + s.RemoteEndPoint.ToString() + " 离线");
}
s.Close();
}
}
else
{
this.DelAccpSocket(s);
if (log.IsInfoEnabled)
{
log.Info(s.GetHashCode().ToString() + " " + s.RemoteEndPoint.ToString() + " 离线");
}
s.Shutdown(SocketShutdown.Both);
s.Close();


}
}

private void DelAccpSocket(Socket accpSocket)
{
lock (this.accpSockets.SyncRoot)
{
this.accpSockets.Remove(accpSocket.GetHashCode());
}
RefrenshUI();
}

private void SendMessage(string msg)
{
if (msg == null)
return;
if (msg == "")
return;
try
{
lock (this.accpSockets.SyncRoot)
{
foreach (System.Collections.DictionaryEntry item in this.accpSockets)
{
Socket accpSocket = (Socket)item.Value;
if (accpSocket != null)
{
if (log.IsInfoEnabled)
{
log.Info("发送信息");
}

byte[] mydata;
mydata = System.Text.Encoding.UTF8.GetBytes(msg);

try
{
accpSocket.Send(mydata, 0, mydata.Length, SocketFlags.None);
//accpSocket.BeginSend(mydata, 0, mydata.Length, SocketFlags.None, new AsyncCallback(this.SendCallBack), accpSocket);
}
catch (Exception ex)
{
if (log.IsErrorEnabled)
{
log.Error(accpSocket.RemoteEndPoint.ToString() + "已经关闭", ex);
}
}
finally
{
mydata = null;
}
}
}
}
}
catch (Exception ex)
{
if (log.IsErrorEnabled)
log.Error("发送信息错误", ex);
MessageBox.Show(ex.ToString());
}
}

private void SendCallBack(IAsyncResult ar)
{
Socket accpSocket = (Socket)ar.AsyncState; ;
try
{
int count = ((Socket)ar.AsyncState).EndSend(ar);
if (log.IsInfoEnabled)
{
log.Info("发送信息完成 length:" + count.ToString());
}
}
catch(Exception ex)
{
if (log.IsErrorEnabled)
{
log.Error("发送失败", ex);
}
}
}
Essence_zhx 2006-09-22
  • 打赏
  • 举报
回复
代码如下:
private void CreateMonitor()
{
try
{
string hostName = Dns.GetHostName();
IPAddress host = Dns.GetHostByName(hostName).AddressList[0];

int port = Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MonitorPort"]);
server = new IPEndPoint(host, port);

listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listeningSocket.Bind(server);

listeningSocket.Listen(100);

if (log.IsInfoEnabled)
{
log.Info(this.hostIPAddress.ToString() + " 开始监听 listeningSocket: " + listeningSocket.GetHashCode().ToString());
}

listeningSocket.BeginAccept(new AsyncCallback(AcceptCallBack), listeningSocket);
}
catch (Exception ex)
{
if (log.IsErrorEnabled)
{
log.Error("建立监听错误", ex);
MessageBox.Show(ex.ToString());
}
}


}
byte[] receiveData = new byte[1024];
private void AcceptCallBack(IAsyncResult ar)
{
try
{
if (log.IsInfoEnabled)
{
log.Info("AcceptCallBack()..............");
}

Socket listener = (Socket)ar.AsyncState;
Socket client = listener.EndAccept(ar);

client.BeginReceive(receiveData, 0, receiveData.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), client);

this.AddAccpSocket(client);

if (log.IsInfoEnabled)
{
log.Info(DateTime.Now.ToLongTimeString() + " " + Thread.CurrentThread.GetHashCode().ToString() + " accpSocket:" + client.GetHashCode().ToString() + client.RemoteEndPoint.ToString());
}

listener.BeginAccept(new AsyncCallback(AcceptCallBack), listener);

}
catch (Exception ex)
{
if (log.IsErrorEnabled)
{
log.Error("接受连接错误", ex);
}
}
}

private System.Collections.Hashtable accpSockets = new Hashtable();

private void AddAccpSocket(Socket accpSocket)
{
if (!accpSockets.Contains(accpSocket.GetHashCode()))
this.accpSockets.Add(accpSocket.GetHashCode(), accpSocket);
this.RefrenshUI();
}
jiezhi 2006-09-22
  • 打赏
  • 举报
回复
没有抛出异常不代表没有错误啊。
写日志看看。

110,565

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧