NET_WriteToStreamSocket(3)
π NET_WriteToStreamSocket(3) β the SDL_net3 glow-up edition
okay bestie, pull up a chair, we're learning how to yeet bytes across the internet π this one lives in the SDL_net3 functions, and it's genuinely one of the most useful little guys in the whole library. no cap.
β¨ NAME β¨
NET_WriteToStreamSocket β send bytes over a stream socket to a remote system. that's it. that's the whole vibe. π
β¨ SYNOPSIS β¨
#include <SDL3_net/SDL_net.h>
bool NET_WriteToStreamSocket(NET_StreamSocket *sock, const void *buf, int buflen);
β¨ DESCRIPTION β¨
okay so first things first: stream sockets are _reliable_. like, that's their whole personality. data you send over them arrives in the exact order it was transmitted, and the system will retransmit stuff as needed to make sure it actually shows up. which basically means: short of catastrophic failure, your data will arrive. possibly with severe delays, bestie. and also β and I need you to hear me on this β "catastrophic failure" is not an uncommon event. π it's giving "your package will arrive, probably, unless the truck explodes, and sometimes the truck explodes."
(this is the opposite of Datagram sockets, which send chunks of data that might arrive in any order, or might not arrive at all β but at least you never sit there waiting for missing chunks to show up. different energy entirely. π¦)
stream sockets are also _bidirectional_ β you can read and write from the same stream, and the other end of the connection can do the exact same thing right back at you. mutual. reciprocal. iconic. π
and here's the part that makes this function so chill: this call never blocks. if it can't send the data immediately, the library just queues it for later transmission. no hanging, no drama, no frozen main loop. if you wanna peek at how much is still sitting in the queue, call NET_GetStreamSocketPendingWrites(). or if you need to actually wait until all that pending data has gone out, NET_WaitUntilStreamSocketDrained() will block for you. pick your fighter. πͺ
now the sad part π₯ β if the connection has failed (remote side dropped us, or one of a million other networking failures occurred), this function reports failure by returning false. stream sockets only report failure for unrecoverable conditions. so once a stream socket fails, assume it is no longer usable and destroy it with NET_DestroyStreamSocket(). it's not coming back. grieve and move on. π«Ά
β¨ FUNCTION PARAMETERS β¨
sockβ the stream socket you're sending data through. your little tube of destiny. πbufβ a pointer to the data to send. the actual goods. πbuflenβ the size of the data to send, in bytes. how much of the goods. π
β¨ RETURN VALUE β¨
returns true if the data was sent or queued for transmission, and false on failure. and if it's a false situation? call SDL_GetError() for the details, because it will absolutely tell you what went wrong. π
β¨ THREAD SAFETY β¨
do not operate on the same socket from multiple threads at the same time without supplying a serialization mechanism. like, that's a whole mess, don't do it. however β different threads may access different sockets at the same time with zero problems. so parallelize responsibly, bestie. β¨
β¨ AVAILABILITY β¨
this function is available since SDL_net 3.0.0. so if you're on anything olderβ¦ babes. upgrade. π
β¨ SEE ALSO β¨
and that's the whole page, periodt. π this was NET_WriteToStreamSocket(3) from the SDL_net manual, page version SDL_net 3.2.0 β go forth and send ur bytes, main character. you've got this. π¦ππΈ
π¦ where this page comes from
- fedora 43: SDL3_net-devel 3.2.0-2.fc43 Β· Zlib
- fedora 43: SDL3_net-devel 3.2.0-2.fc43 Β· Zlib
- fedora 44: SDL3_net-devel 3.2.0-2.fc44 Β· Zlib
- fedora 44: SDL3_net-devel 3.2.0-2.fc44 Β· Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms