[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.4.1 SDLNet_UDP_Open

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.

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.

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.

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

See Also:
3.4.2 SDLNet_UDP_Close, 4.3 UDPsocket


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on November, 3 2009 using texi2html