套接字编程:暂停后向套接字发送数据/接收数据 [英] socket programming: sending data/receiving data to socket after pause
本文介绍了套接字编程:暂停后向套接字发送数据/接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个gui,我使用TcpClient连接到一个设备。一切都有效,除非我在长时间停顿后发送/接收数据。例如,假设我向设备发送hello,然后再等待60秒再发送一次。我会得到
I have this gui where I used TcpClient to connect to a a device. Everything works except when I am sending/receiving data after a long pause. For example, lets say I send "hello" to the device and then wait 60 seconds before I send it again. I would get
InvalidOperationException
或
IOException
这是有意义的,因为套接字时间当它长时间不做任何事情时。有没有办法防止这种情况发生,或者我是否必须捕获这些异常并重新建立连接?
提前感谢
which make sense because the socket times out when its not doing anything for a long period. Is there a way to prevent this from happening or do I have to catch those exceptions and re-establish the connection?
thanks in advance
推荐答案
您需要重新建立连接。通常,TCP连接将在30秒后超时。大多数TCP程序都使用序列open - send - close进行编码。
You need to re-establish connection. Typically a TCP connection will time out after 30 seconds. Most TCP programs are coded with the sequence open - send - close.
您可能想要重新考虑您的gui。只需一个发送按钮,当用户点击它时,连接 - 发送数据 - 然后断开连接。
You may want to rethink your gui. Just have a send button, and when the user hits it, connect - send the data - then disconnect.
这篇关于套接字编程:暂停后向套接字发送数据/接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文