如何在c ++中检测tcp客户端连接到服务器 [英] how to detect tcp client connect to server in c++
问题描述
我有一个tcp客户端/服务器,我想检测客户端的连接丢失;因为我的客户端有多个接口,并且我一次连接到服务器与其中一个,我想知道如何检测客户端连接丢失,以便我可以连接我的TCP客户端与另一个接口到服务器,如果所有下来我把我的数据存储在文本文件中。
I have a tcp client/server, and I want to detect connection loss in client side; because my client have multiple interfaces and at a time I connected to server with one of them, I want to know how to detect connection loss in client side so that I could connect my tcp client with another interface to the server and if all of them are down I store my data in text files. I googled it and I already seen keep alive but it's not what I want.
如果它是重要的我的项目是在linux和代码是在c + +。
if it is important my project is in linux and code is in c++.
推荐答案
我发现的最好的方法是检查缓冲区,如果缓冲区是空的,这意味着你的TCP客户端发送数据包到TCP服务器成功并可以发送下一个数据包;为了检查缓冲区,你可以使用 SIOCOUTQ
;它非常容易使用,并显示您在缓冲区中有多少数据。
The best way that I found is to check buffer, if buffer is empty it means that your TCP client send the packet to the TCP server successfully and you can send the next packet; for checking the buffer you can use SIOCOUTQ
; its very easy to use and show you how much data you have in your buffer.
这篇关于如何在c ++中检测tcp客户端连接到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!