TCP套接字客户端处于接收状态 [英] TCP Socket Client in receiving state

查看:180
本文介绍了TCP套接字客户端处于接收状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个程序,该程序使用连接到服务器的套接字,并等待使用Socket.receive接收数据.

这里的问题是,我需要每60秒发送一条消息,以查看服务器是否仍在运行,然后返回到接收状态.我一直在尝试使用ReceiveTimeout,但是以某种方式关闭了套接字(至少在调试中,我已经看到Socket.Connected == false),就像MSDN所说的那样,它将抛出SocketExeption .HELP PLEASE!

Hello, I have a program that uses a socket that connects to a server, and waits for receiving data using the Socket.receive.

The question here is that, I need to send an message every 60 seconds to see if the server is still alive and then get back to receive state. I''ve been trying to use the ReceiveTimeout, but this, somehow, Closes the Socket (at least in debug i''ve seen that the Socket.Connected == false) and just like the MSDN says, it will throw an SocketExeption.HELP PLEASE!

IPEndPoint EPhost = new IPEndPoint(hostadd, port);
           s.Connect(EPhost);
           Byte[] receive = new Byte[1024];
           s.ReceiveTimeout = 2000;
           while (true)
           {
               try
               {
                   Console.WriteLine("RECEIVING");
                   s.ReceiveTimeout = 2000;
                   s.Receive(receive);
                   
               }
               catch (SocketException sex)
               {
                  //I HAVE NO IDEIA WHAT I SHOULD DO HERE.

               }
           }

推荐答案

您无需停止接收即可发送.

当然,如果服务器将响应发送到您的ping,则必须在接收处理程序中期望它.

尼克
You don''t need to stop your receive to do a send.

Of course, if the server sends a response to your ping, you''ll have to expect that in your receive handler.

Nick


我对套接字不是很熟悉,但是会使用类似Socket.Poll方法的方法来实现您的目的吗? http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.poll.aspx [ ^ ]
I am not very familiar with sockets, but would using something like the Socket.Poll method work for your purposes? http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.poll.aspx[^]


这篇关于TCP套接字客户端处于接收状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆