Howto detect that a network cable has been unplugged in a TCP connection?

前端 未结 3 533
轮回少年
轮回少年 2020-12-12 00:25

I have a C++ networking application that accepts TCP connections from clients and then waits on the socket until the client decides to send data (sometimes they won\'t send

3条回答
  •  温柔的废话
    2020-12-12 00:52

    Your question is quite complex. There are a lot of things that can go wrong between you and your client. More then just a 'unplugged' cable.

    If you simply want to know if your user is still online you can build a new TCP connection. Because you need to complete the 3-way handshake to successfully build a TCP connection you know the client is online when the connection is successfully initialized. The problem with this is, that if you want to keep your current connection active you need an other port. Don't know if this is an issue in your case.

    But by the sounds of it you are not really sending and receiving data from your client (apart from some ping data). So you can simply set your application in a loop to setup a TCP connection (the first 2 steps - so receiving the ACK - should be enough to determine if your client is still processing network data) every X amount of seconds. If you don't get a response in X milliseconds you can quite reliably say that either your client or something in between stopped "working".

    Hope this helps. If not, please give some more info on what your tool is doing.

提交回复
热议问题