sdlNetCreateServer function

Pointer<SdlNetServer> sdlNetCreateServer(
  1. Pointer<SdlNetAddress> addr,
  2. int port
)

Create a server, which listens for connections to accept.

An app that initiates connection to a remote computer is called a "client," and the thing the client connects to is called a "server."

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.

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. If this returns success, you can immediately start accepting connections.

You can specify an address to listen for connections on; this address must be local to the system, and probably one returned by SDLNet_GetLocalAddresses(), but almost always you just want to specify NULL here, to listen on any address available to the app.

After creating a server, you get stream sockets to talk to incoming client connections by calling SDLNet_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.

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.

\param addr the local address to listen for connections on, or NULL. \param port the port on the local address to listen for connections on. \returns a new SDLNet_StreamSocket, pending connection, or NULL on error; call SDL_GetError() for details.

\threadsafety It is safe to call this function from any thread.

\since This function is available since SDL_Net 3.0.0.

\sa SDLNet_GetLocalAddresses \sa SDLNet_AcceptClient \sa SDLNet_DestroyServer

extern SDL_DECLSPEC SDLNet_Server * SDLCALL SDLNet_CreateServer(SDLNet_Address *addr, Uint16 port)

Implementation

Pointer<SdlNetServer> sdlNetCreateServer(
    Pointer<SdlNetAddress> addr, int port) {
  final sdlNetCreateServerLookupFunction = libSdl3Net.lookupFunction<
      Pointer<SdlNetServer> Function(Pointer<SdlNetAddress> addr, Uint16 port),
      Pointer<SdlNetServer> Function(
          Pointer<SdlNetAddress> addr, int port)>('SDLNet_CreateServer');
  return sdlNetCreateServerLookupFunction(addr, port);
}