如何在UDP应用程序中关闭客户端或服务器时 [英] How to handle when a Client or Server is Down in a UDP Application

查看:469
本文介绍了如何在UDP应用程序中关闭客户端或服务器时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UDP开发用于客户端服务器通信的Windows应用程序,但由于UDP是无连接的,因此每当客户端关闭时,服务器都不知道客户端已关闭并继续发送数据。服务器关闭时的情况类似。我怎样才能满足这样的条件,即当任何客户端或服务器出现故障时,另一方必须知道它并且可以处理它。等待回复。

I am developing a windows application for Client Server communication using UDP, but since UDP is connectionless, whenever a Client goes down, the Server does not know that Client is off and keeps sending the data. Similar is the case when a Server is down. How can I cater this condition that whenever any of the Client or Server is down, the other party must know it and can handle it. Waiting for reply.

推荐答案

我多年前在VB6中做过这个,但我记得如果他们无法联系机器名,就会抛出异常。但是,如果机器在线,但你的应用程序没有收听,那么我所做的就是实现一个轮询情况,所以如果我在一定时间内没有得到回复,那么它就会被假定为离线。您的另一个选择是使用TCP。
I did this many years ago in VB6 but as I recall an exception was thrown if they could not contact the machine name. However, if the machine is online but your app is not listening then what I did was implement a polling situation so if I did not get a response back in a certain amount of time it was assumed offline. Your other option is to use TCP.


这取决于使用的通信。



当对方定期发送数据包时,您可以在特定时间内没有收到数据时停止发送数据包,并在下次接收时继续发送。如果没有定期发送数据,只需发送它们即可。这种分组称为保活分组。如有必要,在程序启动时发送数据包以指示客户端/服务器存在并触发另一端开始发送。



使用上面的内容,你会发送数据包,直到发生超时。但这通常不是问题。如果您的通信协议可以更改,您可能会发送额外的特殊数据包,表明服务器或客户端正在关闭,以便另一方可以立即停止。
This depends on the used communication.

When the other side sends packets in regular intervals, you can stop sending packets when no data are received for a specific time and resume sending upon the next receive. If no data are send in regular intervals, just send them. Such packets are called keep-alive packets. If necessary, send a packet at program start to indicate that the client / server is present and trigger the other side to start sending.

Using the above, you will have packets send until the time out occurs. But this is normally not a problem. If your communication protocol can be changed, you might send additional special packets indicating that a server or client is going down so that the other side can stop immediately.


您可以设置自己的握手UDP之上的协议。换句话说,让接收方发送一个发送方将处理的响应。
You can set up your own handshake protocol on top of UDP. In other words, have the receiving machine send a response that the sender will process.


这篇关于如何在UDP应用程序中关闭客户端或服务器时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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