Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to, keepalive (TCP)  (Read 2587 times)

0 Members and 1 Guest are viewing this topic.

Morzie

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
How to, keepalive (TCP)
« on: August 24, 2016, 08:27:28 am »
I'm relatively new to networking in general but I've been looking into networking as a kind of side project.
Anyways, I've been trying to solve the problem of the client suddenly disconnecting in a way that doesn't allow SFML to automatically tell the server it has disconnected, and thus the server has continues to wait indefinitely (I think there's a timeout built into windows but I think it's like 7 hours, which is way too long).
So I'm trying to find a way to implement keepalive packets but have found nothing about in with SFML and I can't seem to find a way to tinker with SFML's packet's raw data?

Any suggestions or resolutions to the problem?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to, keepalive (TCP)
« Reply #1 on: August 24, 2016, 08:40:05 am »
Quote
the client suddenly disconnecting in a way that doesn't allow SFML to automatically tell the server it has disconnected
The next read or write to the disconnected client will fail and SFML will always report the error. So what is this "way" that doesn't allow the server to detect the disconnection? Do you mean that no read or write happens, and yet you want to know that the client is not there anymore as soon as he's disconnected?

In this case what you want is not keep-alive: you don't want to keep the client connected, but rather detect when he's disconnected. This is quite simple: implement a kind of ping-pong (often called "hartbeat" in this context), ie. the client and server exchanging simple packets just to let the other side know that they still respond. Then you have to define a delay after which the client can be considered disconnected if he hasn't sent anything back.
Laurent Gomila - SFML developer

Morzie

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: How to, keepalive (TCP)
« Reply #2 on: August 24, 2016, 09:08:48 am »
Quote
implement a kind of ping-pong (often called "hartbeat" in this context)
This is what I'm taking about when I say "keepalive", it doesn't mean to keep the client alive, it's sending an empty packet with the ACK flag set and wait for the client to respond, and if they don't, close the socket, however I don't know how to access the raw TCP data to set the ACK flag or check if it is set on the client's side?

http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to, keepalive (TCP)
« Reply #3 on: August 24, 2016, 10:13:21 am »
SFML doesn't allow to configure the TCP keepalive. Even if it did, I don't know how it signals a disconnection, but if it's with an error flag, SFML won't be able to forward it to you.

The easiest way is to implement the feature yourself with regular packets.
Laurent Gomila - SFML developer

Morzie

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: How to, keepalive (TCP)
« Reply #4 on: August 25, 2016, 02:52:44 am »
Quote
The easiest way is to implement the feature yourself with regular packets.
So would you suggest creating my own packet class deriving from SFML's packet or ditch SFML and pick up something like Winsock for networking?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to, keepalive (TCP)
« Reply #5 on: August 25, 2016, 07:46:58 am »
It's really easy to implement, you don't have to create a packet class nor use Winsock. Just create a simple protocol (or, most likely, add a new type of request to the protocol you're already using) to exchange those ping-pong messages and use a clock that will notify a disconnection when no data has been received for more than a few seconds.
Laurent Gomila - SFML developer

 

anything