NET_CreateServer(3)
β¨ NET_CreateServer β¨
okay bestie, welcome to the SDL_net 3 manual, page NET_CreateServer(3) π this one's literally the "let me be the one everyone connects to" moment, main character energy fr fr π
πΈ NAME πΈ
NET_CreateServer β create a server, which listens for connections to accept. no cap, that's the whole job description π
π¦ SYNOPSIS π¦
#include <SDL3_net/SDL_net.h>
NET_Server * NET_CreateServer(NET_Address *addr, Uint16 port, SDL_PropertiesID props);
#define NET_PROP_SERVER_REUSEADDR_BOOLEAN "NET.server.reuseaddr"
π DESCRIPTION π
okay so first, vocab check π An app that initiates connection to a remote computer is called a "client," and the thing the client connects to is called a "server." that's it, that's the dynamic π
Servers listen for and accept connections from clients, which spawns a new stream socket on the server's end, which it can then send/receive data on. cute, right? π«Ά
Use this function to create a server that will accept connections from other systems. π
This function does not block, and is not asynchronous, as the system can decide immediately if it can create a server or not. So if this returns success, you can immediately start accepting connections. iconic behavior honestly β
You can specify an address to listen for connections on; this address must be local to the system, and probably one returned by NET_GetLocalAddresses(), but almost always you just want to specify NULL here, to listen on any address available to the app. like, just let it cook π₯
After creating a server, you get stream sockets to talk to incoming client connections by calling NET_AcceptClient(). π§
Stream sockets don't employ any protocol (above the TCP level), so they can accept connections from clients that aren't using SDL_net, but if you want to speak any protocol beyond an abritrary stream of bytes, such as HTTP, you'll have to implement that yourself on top of the stream socket. yes bestie, the HTTP is a you problem, the library said good luck π
Unlike BSD sockets or WinSock, you specify the port as a normal integer; you do not have to byteswap it into "network order," as the library will handle that for you. we love a library that does the annoying part for us π
The caller may supply properties to customize behavior. This is optional, and a value of zero for props will request defaults for all properties.
These are the supported properties: π
NET_PROP_SERVER_REUSEADDR_BOOLEAN:trueif the server should be created even if a previous server has recently used this address. For various reasons, networks prefer that there be some delay between apps reusing the same address, but this can be problematic when iterating quickly, for software development purposes or just restarting a crashed service. This property defaults totrue(although it should be noted that, at the operating system level, this defaults tofalse!). If this property isfalseand the OS feels that not enough time has elapsed, server creation will fail and this function will report an error. π
bestie note the plot twist: SDL_net defaults this totrue, but the OS-level default isfalse. do not get it twisted π
π FUNCTION PARAMETERS π
- addr β the local address to listen for connections on, or
NULL. - port β the port on the local address to listen for connections on.
- props β properties of the new server. Specify zero for defaults.
β RETURN VALUE β
Returns a new NET_Server, or NULL on error; call SDL_GetError() for details. and when it errors you WILL be calling SDL_GetError(), that's not a suggestion that's a lifestyle π
π THREAD SAFETY π
It is safe to call this function from any thread. yes queen, thread-safe, go off in parallel π
π AVAILABILITY π
This function is available since SDL_net 3.0.0. so if you're on something older, sorry babe π
π«Ά SEE ALSO π«Ά
and that's the whole page, bestie!! β¨ this was SDL_net 3.2.0 (the SDL_net manual, mwah π) β go create a server, accept some clients, and remember: NULL for addr is almost always the move. periodt ππ
π¦ 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