You will use socket programming to write an echo server that utilizes the TCP/IP protocol. You will recall that Transmission Control Protocol (TCP) is connection-oriented, implements byte stream communications, provides reliable delivery using error and flow control, and supports full duplex communications.
In this part of the project, you will learn in more detail about socket functions and the data structures and transformation functions used in socket programming. You will implement the TCP echo server using a client-server model. The TCP echo server is an implementation of the Echo Protocol which is described in RFC 862.
A socket is a communication mechanism and is identified by an integer that is called the socket descriptor. A socket includes a data structure consisting of the following five data items:
Server applications run with specific port numbers, and client applications contact the server applications using the server port numbers and the server IP address.
There are a number of functions used in socket programming. These are:
Table 1: Socket Programming Functions
socket() | create a socket |
bind() | associate a socket with a server port number |
connect() | connect a socket to a server system with its address |
listen() | wait for a connection request from a client system |
accept() | accept the connection request |
In addition to the functions listed in Table 1, the functions read() and write() may be used to send data. The function close() may be used to close a socket. A socket call does not specify where data will be coming from, nor where it will be going. A socket call just creates the socket interface!
For socket programming using TCP/IP, we must include the following header files:
#include < sys/types.h> /* library of basic types */
#include < sys/socket.h> /* library of socket functions */
#include < netinet/in.h> /* library of Internet address functions */
#include < arpa/inet.h> /* library of functions for Internet operations */
An IP socket address is defined as a combination of an IP interface address and a 16-bit port number. The basic Internet Protocol (IP) does not supply port numbers; they are implemented by higher-level protocols like TCP.
struct sockaddr_in {
short int sin_family; /* address family: AF_INET for IPV4 */
short unsigned int sin_port; /* port in network byte order */
struct in_addr sin_addr; /* a struct in_addr to store the IP address */
unsigned char sin_zero[8]; /* not used; set to NULL */ };
int sockid= socket(family, type, protocol);
/* sockid is like a file-handle, has a value of -1 when socket creation fails */
/* family: AF_INET or PF_INET (AF=address family, PF=protocol family) */
/* type: SOCK_DGRAM for UDP, SOCK_STREAM for TCP */
/* protocol: IPPROTO_UDP, IPPROTO_TCP or 0 (default) */
You must complete the tcp_echo_client.c and tcp_echo_server.c programs. You can find the source code in zip file attachment below. The source modules will compile and run, but the program functionality is incomplete. To complete these programs, you should implement the following requirements:
You will need to test your programs on two terminals, one terminal for the server and another terminal for the client. The port number and IP address for the client should be specified as command line arguments.
# Compile the TCP echo server program
$ gcc tcp_echo_server.c -o tcp_echo_server
# Run the TCP Echo server program from the command line
./tcp_echo_server
# Compile the TCP echo client program
$ gcc tcp_echo_client.c -o tcp_echo_client
# Run the client program from the command line; specify the IP address, port number and message
./tcp_echo_client 127.0.0.1 8899 message
If there are no errors in your implementation of the TCP echo program, then tcp_echo_server will print the message sent from tcp_echo_client.