Create a UDP socket
a valid UDPsocket on success. NULL is returned on errors, such as when it's not able to create a socket, or it cannot assign the non-zero port as requested.
Binds to C-function call in SDL_net.h:
extern DECLSPEC UDPsocket SDLCALL SDLNet_UDP_Open(Uint16 port)
Note that below I say server, but clients may also open a specific port, though it is prefered that a client be more flexible, given that the port may be already allocated by another process, such as a server. In such a case you will not be able to open the socket, and your program will be stuck, so it is better to just use whatever port you are given by using a specified port of zero. Then the client will always work. The client can inform the server what port to talk back to, or the server can just look at the source of the packets it is receiving to know where to respond to.
// create a UDPsocket on port 6666 (server) UDPsocket udpsock; udpsock=SDLNet_UDP_Open(6666); if(!udpsock) { printf("SDLNet_UDP_Open: %s\n", SDLNet_GetError()); exit(2); }
// create a UDPsocket on any available port (client) UDPsocket udpsock; udpsock=SDLNet_UDP_Open(0); if(!udpsock) { printf("SDLNet_UDP_Open: %s\n", SDLNet_GetError()); exit(2); }
SdlNet Class | Tao.Sdl Namespace | SDLNet_UDP_Close | SdlNet.UDPsocket