UDP/IP Sockets Demo 2

Demo 2: Binding a socket

This sample program illustrates the creation of a UDP socket using the socket system call and binding it to any available port. It then uses the getsockname system call to print the port number that the operating system assigned to the socket. It doesn't do anything beyond that.

To run it, download the demo file and unzip it to create the demo-udp-02 directory. Then compile the file by running make or manually with:

cc -o demo-udp-02 demo-udp-02.c

and run it:

./demo-udp-02

The ./ prefix is there just in case you don't have the current directory in your search path (it's a good security practice not to). If you're on a SunOS (System V) machine, you'll need to link with the socket library by compiling with:

cc -o demo-udp-02 demo-udp-02.c -lsocket

You'll need to edit the makefile and uncomment the LIBS definition.

Download the demo file (zip)