Inter process communication(IPC) using internet protocol sockets are one way of communicating between two processes i.e. a client process and a server process in linux. We will use Internet sockets which are also called Berkeley or BSD sockets. BSD sockets can only be used to communicate between two processes on the same host linux machine.
One application of BSD sockets is in command line interface(CLI) development to provide an user interface to an electronic product. The client process sends a command to be executed to the server process using socket. The server process actually executes the command and provides the response back to the client process.
The steps involved in establishing a TCP socket on the server side are as follows:
Create a socket with the socket() function
Bind the socket to an address using the bind() function
Listen for connections with the listen() function
Accept a connection with the accept() function system call. This call typically blocks until a client connects with the server.
Send and receive data by means of send() and receive().
The steps for establishing a TCP socket on the client side are the following:
Create a socket using the socket() function
Connect the socket to the address of the server using the connect() function
Send and receive data by means of the read() and write() functions.
Let's go through the client code written in client.c. We created a socket using createSocket function. We used connectSocket function to make a connection to the server. We used server port as 8090 in both client and server programs. Client and server runs on same machine having same local host IP address as 127.0.0.1.
The getCommand function is written to get a command from user through stdin. sendSocket function is used to finally send the user command to the server