Simple example of use of sockets

Berkeley sockets, or simply sockets, is a transport layer software available across several computing platforms. The two C program files, sock_cln.c and sock_srvr.c together provides a very simple example of use of sockets. The programs have been adopted from examples given in the book Computer Networks by A S Tanenbaum, 4e. sock_cln.c contains a simple client program, and sock_srvr.c contains a simple server program suitable for the client.

The two programs may be copied, compiled and run on the same machine or two different machines that are connected by a network.

To compile the client program (in a linux machine)-

make sock_cln

To compile the server program (in a linux machine)-

make sock_srvr

To run the server program-

sock_srvr "at your service"

To run the client program-

sock_cln <server-name> "Hello"
where <server-name> may be some name such as venus.tezu.ernet.in, and the double quoted strings in the two commands may be any string.

To know more about sockets refer to the books

  1. Computer Networks by A S Tanenbaum, 4e
  2. Unix Network Programming by W R Stevens, Vol 1

Find out

  1. What do the above two programs do?
  2. What are argc and argv used for? How?
  3. What do the following header files contain-
    <sys/types.h>, <sys/socket.h>, <netinet/in.h>, <netdb.h> <sys/fcntl.h>
  4. What do the following functions do-
    gethostbyname( ),    socket( ),    memset( ),    memcpy( ),    connect( ),    setsockopt( ),    bind( ),    listen( ),    accept( ),    htonl( ),    htons( ),   
  5. What are the different parameters of the above functions?
  6. What do the following structures hold-
    struct hostent,       struct sockaddr_in
  7. What values may be used as port numbers?
  8. What does QUEUE_SIZE used in the server program signify?
  9. How would you make the server program terminate without killing it?

Try out the following by modifying the given programs:

  1. Send a file from the client to the server, and from the server to the client. What about the file-name at the destination?
  2. Make an interactive text communication tool.
  3. Get the file-listing (like ls) of a directory at the server from the client.