1

Is there any way to cancel a SDLNet_TCP_Recv call after some time when no answer arrives? Or are there alternatives to this function that support a timeout option?

user11914177
  • 885
  • 11
  • 33

1 Answers1

3

Create a socket set with SDLNet_AllocSocketSet() & add your TCPsocket to it with SDLNet_AddSocket(). Then you can use SDLNet_CheckSockets() with a timeout to wait for network activity.

Depending on the return-value you can then use SDLNet_SocketReady() to verify that the SDLNet_TCP_Recv() call won't block.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • I don't really get it how `SocketSet` are supposed to be used. Should they replace a `std::vector` of `TCPsocket`? How can the single elements be accessed? – user11914177 Nov 10 '19 at 11:19
  • @user11914177: SocketSet is essentially a wrapper around [a POSIX `fd_set`](https://en.wikipedia.org/wiki/Select_%28Unix%29). You can't really iterate over a SocketSet though SDL_net's API so you'll want to keep that `std::vector` of sockets around. – genpfault Nov 10 '19 at 20:23
  • Ok, but how can I use `SDLNet_CheckSockets`? How do I know which sockets are no longer active? It is returning only the amount of active sockets... – user11914177 Nov 11 '19 at 12:58
  • @user11914177: After `SDLNet_CheckSockets()` returns >=1 you iterate over your `std::vector` of sockets & call `SDLNet_SocketReady()` on each one. If that returns true you're free to read that socket without it blocking. – genpfault Nov 11 '19 at 14:48
  • Ok, that is working. Is there anything that can be done with the return of `SDLNet_SocketReady`? Can this be used for anything or is it just returning -1, 0 and 1? – user11914177 Nov 12 '19 at 15:37
  • @user11914177: Not sure I follow, you should already be using the return-value of `SDLNet_SocketReady()` to determine socket readiness. – genpfault Nov 12 '19 at 15:51
  • Yes, I already use it, but if the number returned is bigger than zero does this number indicate anything else than just that there are bytes in the buffer? – user11914177 Nov 12 '19 at 15:56
  • @user11914177: Nope, it only indicates that there's bytes in the buffer. – genpfault Nov 12 '19 at 16:00
  • Ok thanks for all that answers, now I know much more about SDLNet! – user11914177 Nov 12 '19 at 16:06