TCP/IP Sockets Demo 2

Demo 2: Binding a socket

This sample program illustrates the creation of a TCP/IP 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-02 directory. Then compile the file by running make or manually with:

gcc -o demo-02 demo-02.c

and run it:

./demo-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:

gcc -o demo-02 demo-02.c -lsocket

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

Download the demo file (zip)