0

How can I check if a SDLnet TCPsocket has closed the connection? I tried checking if the socket is NULL but it doesn't work, this is what I tried:

    for(int i = 0; i < clientConnections.size(); i++) {
        if(!clientConnections[i]) {
            clientConnections.erase(clientConnections.begin() + i);
            i--;
        }
    }

Also I haven't found anything in the documentation.

user11914177
  • 885
  • 11
  • 33
  • 1
    Pointers doesn't "magically" becomes null, and objects don't just destruct themselves (or rather, they shouldn't). The common way is to poll the socket, and if it's readable and the next attempt to read from the socket returns that it read zero bytes, that means the other end have closed the connection in a nice way. – Some programmer dude Oct 31 '19 at 13:14
  • @Someprogrammerdude this isn't very useful for me. I don't expect something that is sent by the clients currently. – user11914177 Oct 31 '19 at 14:15
  • Well that's kind of the point. The act of the connection being closed means the socket on your side will be marked as readable, and that the next read operation will return with zero bytes read. That's the way to discover closed connections. – Some programmer dude Oct 31 '19 at 14:17
  • @Someprogrammerdude SDLnet will wait until something gets received so this is useless – user11914177 Oct 31 '19 at 14:29
  • 1
    Socket sets, [`SDLNet_CheckSockets`](https://www.libsdl.org/projects/SDL_net/docs/SDL_net_46.html#SEC46) and [`SDLNet_SocketReady`](https://www.libsdl.org/projects/SDL_net/docs/SDL_net_47.html#SEC47)? From the `SDLNet_CheckSockets` documentation: "**NOTE:** "activity" also includes ***disconnections***..." (extra emphasis mine) – Some programmer dude Oct 31 '19 at 14:34
  • @Someprogrammerdude I don't use socket sets and I don't really plan to do so... – user11914177 Oct 31 '19 at 14:36
  • Then how do you currently poll for socket activity? How do you currently know is a socket have received data? Perhaps your basic design premise needs to be looked over? If you want to discover closed connections you ***must*** read from the socket, that's really the only way to discover a nicely closed connection. Well, you *can* attempt to write to the socket and get an error, but reading from the socket is really the official and nice way to discover disconnections. – Some programmer dude Oct 31 '19 at 14:38
  • @Someprogrammerdude Perhaps I haven't stated enough, that I want a function from SDLnet to check that and not to implement that by my self! – user11914177 Oct 31 '19 at 14:46
  • 1
    Then no, there's no utility function which does all of that. You have to implement it yourself. – Some programmer dude Oct 31 '19 at 14:49
  • @Someprogrammerdude finally a satisfying answer. Would you write that as an answer so that I can close the question? – user11914177 Oct 31 '19 at 14:54

1 Answers1

1

There's no single utility function in SDLNet to check for a disconnection status, as it's really nothing more than a thin platform-independent wrapping around the underlying platforms network functionality which almost universally doesn't have such direct functionality.

With the SDLNet API it could be implemented using socket sets, which (as is the nature of SDLNet) wraps similar functionality of the most common OS network facilities.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621