Background Information - Socket Programming

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.

Introduction to Sockets

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:

  • Protocol id
  • The destination host address
  • The destination port number, if applicable
  • The local host address
  • The local port number, if applicable

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!

IPv4 Address Structures

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 */

Socket Data Structure

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 */ };

Some Useful IPv4 Address Transformation Functions

  • inet_ntoa() used to convert a struct in_addr to a dotted quad string: "Network-to-Address"
  • inet_aton() used to convert a string of an IP address as a dotted quad into a struct in_addr: "Address-to-Network"
  • getaddrinfo() used to convert a domain name into an IP address, stored in the ai_addr member as a struct sockaddr in the struct addrinfo result
  • htons() used to convert a short int stored in host byte order into a network byte order: "Host-to-Network"
  • ntohs() used to convert a short int stored in network byte order to host byte order: "Network-to-Host"

Socket Creation in C

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) */

TCP Echo Server Implementation

  • In the TCP echo server, we create a socket and bind it to a port number.
  • After binding, the server waits and listens for incoming connections.
  • When received, the server accepts a connection from the client machine.
  • The server receives data from the client using recv() function and echoes the same data back using the send() function.

TCP Echo Client Implementation

  • In the TCP echo client, we create a socket.
  • We specify the client address, the port number, and the message to be sent using values passed from the command line.
  • We send messages to the server using the send() function and receive data from the server using the recv() function.

Project Requirements

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:

  • Server - add code to listen for connections
  • Server - add code to accept connections from a client
  • Server - add code to receive data from a client connection
  • Server - add code to echo data received by the server back to the client
  • Client - add code to send data to the server
  • Client - add code to receive data from the server
  • Client - print the data received from the server
  • Add additional print statements to your code to provide information about what the client and server programs are doing when they run.

Compile and Run the Echo Client and Server Programs

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.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.