Tao.Sdl SDK Documentation

SdlNet.SDLNet_UDP_Open Method 

Create a UDP socket

[Visual Basic]
Public Shared Function SDLNet_UDP_Open( _
   ByVal port As Short _
) As IntPtr
[C#]
public static IntPtr SDLNet_UDP_Open(
   short port
);

Parameters

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.

Return Value

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.

Remarks

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. If 'port' is non-zero, the UDP socket is bound to a local port. The 'port' should be given in native byte order, but is used internally in network (big endian) byte order, in addresses, etc. This allows other systems to send to this socket via a known port.

Binds to C-function call in SDL_net.h:

            extern DECLSPEC UDPsocket SDLCALL SDLNet_UDP_Open(Uint16 port)
            

Example

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

SdlNet Class | Tao.Sdl Namespace | SDLNet_UDP_Close | SdlNet.UDPsocket