socket同步接收数据时,经常报异常,很不稳定,求解!!!

niuniu 2015-08-24 08:52:16
异常:已引发: "由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。"
(System.Net.Sockets.SocketException) 引发了一个
System.Net.Sockets.SocketException:
"由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。" 时间: 2015/6/25 21:29:42 线程:
<无名称>[10136]

while (validate)
{
Thread.Sleep(500);
int readsize;
bool success;
IPEndPoint clientipe = (IPEndPoint)listensocket2.RemoteEndPoint;
byte commandByte = Hex.HexStringToByteArray(command_8);
Logger.Info("D:\\Log\\" + hostcode + ".txt", "循环5.1. 发送命令\n");
listensocket2.Send(commandByte, 0, commandByte.Length, SocketFlags.None);
hostList = AddClient(hostcode, listensocket2, clientipe.Address.ToString(), DateTime.Now, DateTime.Now); //加入客户端对象列表
listensocket2.ReceiveTimeout = 5000;
int countSocketException = 0;
//try
//{
Logger.Info("D:\\Log\\" + hostcode + ".txt", "循环5.2. 接收数据前" + buffer_receive + "|||listensocket2状态:" + listensocket2.Connected + "|||" + listensocket2. + "\n");
readsize = listensocket2.Receive(buffer_receive);
Logger.Info("D:\\Log\\" + hostcode + ".txt", "循环5.3. 接收数据后\n");
...全文
455 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
niuniu 2015-08-25
  • 打赏
  • 举报
回复
还有,我使用的是同步机制。
niuniu 2015-08-25
  • 打赏
  • 举报
回复
回楼上: 1. 网络是通过gprs dtu透传,是否掉包不确定. 2.耗时操作就是receive比较慢. 希望有进一步解答.
niuniu 2015-08-25
  • 打赏
  • 举报
回复
补充一下完整代码,不知道为啥没有权限编辑我的帖子: Part I: protected override void OnStart(string[] args) { IPEndPoint endpoint = new IPEndPoint(RemsSocket.myip, RemsSocket.webport); Socket listensocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); listensocket.Bind(endpoint); listensocket.Listen(200); Thread th = new Thread(new ParameterizedThreadStart(RemsSocket.listen_send)); th.IsBackground = true; th.Start(listensocket); } Part II: public static void listen_send(object source) { while (true) { done.Reset(); Socket listensocket = (Socket)source; listensocket.BeginAccept(new AsyncCallback(acceptcallback_send), listensocket); done.WaitOne(); } } public static void acceptcallback_send(IAsyncResult ar) { done.Set(); Socket socket = (Socket)ar.AsyncState; Socket listensocket2 = socket.EndAccept(ar); byte[] buffer_validate = new byte[buffersize]; string hostcode = ""; byte[] buffer_send = Hex.HexStringToByteArray(validate_code); listensocket2.Send(buffer_send, 0, buffer_send.Length, SocketFlags.None); listensocket2.Receive(buffer_validate); int readsize = buffer_validate.Length; //listensocket2.EndReceive(ar); if (readsize > 0) { bool success = FirstValidate(buffer_validate, ref hostcode); bool validate = BLL.ExtHostBLL.CanCommunicate(hostcode); if (success && validate) { LoopSendReceive(hostcode, listensocket2); } } } Part III: public static void LoopSendReceive(string hostcode, Socket listensocket2) { listensocket2.Blocking = true; byte[] buffer_receive = new byte[buffersize]; bool validate = BLL.ExtHostBLL.CanCommunicate(hostcode); while (validate) { Thread.Sleep(500); int readsize; bool success; IPEndPoint clientipe = (IPEndPoint)listensocket2.RemoteEndPoint; byte commandByte = Hex.HexStringToByteArray(command_8); listensocket2.Send(commandByte, 0, commandByte.Length, SocketFlags.None); listensocket2.ReceiveTimeout = 5000; int countSocketException = 0; readsize = listensocket2.Receive(buffer_receive); if (readsize != 0) { clientipe = (IPEndPoint)listensocket2.RemoteEndPoint; ClientDto client = hostList.FirstOrDefault(c => c.Ip == clientipe.Address.ToString()); ParseChannel.AlertStatus[] data; ParseChannel channel = new ParseChannel(); success = channel.Parse(buffer_receive, client, out data); if (!success) { continue; } SaveData(data, client); } } }
孤独de猫 2015-08-24
  • 打赏
  • 举报
回复
从提示上看,象是socket心跳包没有在规定时间定相互通讯。 1)检查与目标主机之间,是否会出现网络掉包的情况 2)检查客户端与服务器代码,是否有比较耗时的操作。
niuniu 2015-08-24
  • 打赏
  • 举报
回复
这帖好像很我写的做法不一样,还是我太弱,反正没看懂。
niuniu 2015-08-24
  • 打赏
  • 举报
回复
这帖好像很我写的做法不一样,还是我太弱,反正没看懂。
niuniu 2015-08-24
  • 打赏
  • 举报
回复
别沉啊,求助!这个问题折磨我好久了

110,561

社区成员

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

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

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