NET_GetConnectionStatus(3)

πŸ“¦
version 3.2.0 Β· 2 versions
, in fedora 43, fedora 44

πŸ’« NET_GetConnectionStatus(3) πŸ’«

okay bestie, welcome to the SDL_net3 FUNCTIONS corner of the manual, where we talk about NET_GetConnectionStatus β€” the lil function that peeks at your socket and tells you if it's connected yet, no blocking, no waiting around, no drama πŸ’…


✨ NAME ✨

NET_GetConnectionStatus β€” check if a stream socket is connected, without blocking. πŸ¦‹


✨ SYNOPSIS ✨

#include <SDL3_net/SDL_net.h>

NET_Status NET_GetConnectionStatus(NET_StreamSocket *sock);

✨ DESCRIPTION ✨

so here's the tea πŸ“ β€” the NET_StreamSocket objects you get back from NET_CreateClient do not connect instantly. they take time to go negotiate a connection with a server. and because waiting around for an indefinite amount of time is not the vibe, it does all that work _asynchronously_ instead of making your program sit there like 🧍

this function is your little window into that progress. it lets you check how the connection work is going without blocking. obsessed.

⚠️ but listen β€” a connection can totally fail after a while (maybe the server took a sec to respond and then was like "no πŸ’€" and rejected you). so fr fr, actually check the result of this function. do not just assume it worked because the value is non-zero. that's the trap. πŸ™ˆ

once a connection is successfully made, the stream socket is ready to send and receive data with the server. slay. πŸŽ€

and one more thing, iykyk: if the connection succeeds but later gets dropped, this function will still report the connection as successful. that's because it only deals with the initial asynchronous work of getting connected, periodt. you'll find out the connection dropped later, when your reads and writes start reporting failures. 😭


✨ FUNCTION PARAMETERS ✨

  • sock β€” the stream socket you wanna query. πŸ‘‘

✨ RETURN VALUE ✨

  • returns NET_SUCCESS if it's successfully connected πŸŽ‰
  • returns NET_FAILURE if the connection failed πŸ’€
  • returns NET_WAITING if it's still connecting ⏳

and if you got NET_FAILURE, go call SDL_GetError() for the details. it'll spill everything.


✨ THREAD SAFETY ✨

do not operate on the same socket from multiple threads at the same time unless you bring your own serialization mechanism, bestie. that's a recipe for chaos. πŸ™…

however! different threads can absolutely access different sockets at the same time with zero problems. main character energy per socket, basically. πŸ‘‘


✨ AVAILABILITY ✨

this function has been available since SDL_net 3.0.0. it's been iconic for a while now. ⭐


✨ SEE ALSO ✨


and that's the whole page, no cap πŸ’– this was SDL_net 3.2.0, brought to you by the SDL_net girlies. go forth and check your connection status responsibly, mwah πŸ’‹πŸŒΈ

πŸ“¦ where this page comes from