SDLNet_UDP_Open

Name

SDLNet_UDP_Open -- Create a UDP socket

Synopsis

UDPsocket SDLNet_UDP_Open(Uint16 port);

port This is the port number (in native byte order) on which to receive UDP packets. Most servers will want to use a known port number here so that clients can easily communicate with the server. This can also be zero, which then opens an anonymous unused port number, to most likely be used to send UDP packets from.

Description

Open a socket to be used for UDP packet sending and/or receiving. If a non-zero port is given it will be used, otherwise any open port number will be used automatically. Unlike TCP sockets, this socket does not require a remote host IP to connect to, this is because UDP ports are never actually connected like TCP ports are.

This socket is able to send and receive directly after this simple creation.

Return Value

Returns: 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.

Examples

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. However, if you want to communicate with clients behind NAT you'd better look at the source of the packets, because NAT routers might change the client port number as the packet passes the gateway.

// 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);
}

See Also

SDLNet_UDP_Close, UDPsocket

SDLNet_UDP_Open (last edited 2008-04-17 08:18:22 by localhost)