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
- Computer Networks by A S Tanenbaum, 4e
- Unix Network Programming by W R Stevens, Vol 1
Find out
- What do the above two programs do?
- What are argc and argv used for? How?
- What do the following header files contain-
<sys/types.h>, <sys/socket.h>, <netinet/in.h>,
<netdb.h> <sys/fcntl.h>
- What do the following functions do-
gethostbyname( ),
socket( ),
memset( ),
memcpy( ),
connect( ),
setsockopt( ),
bind( ),
listen( ),
accept( ),
htonl( ),
htons( ),
- What are the different parameters of the above functions?
- What do the following structures hold-
struct hostent,
struct sockaddr_in
- What values may be used as port numbers?
- What does QUEUE_SIZE used in the server program signify?
- How would you make the server program terminate without killing
it?
Try out the following by modifying the given programs:
- Send a file from the client to the server, and from the server to the
client. What about the file-name at the destination?
- Make an interactive text communication tool.
- Get the file-listing (like ls) of a directory at the server from
the client.