A POSIX Socket or simply a Socket is defined as a communication endpoint. For example, if two parties, A and B, intend to communicate with each other, then it will be required that both of these parties establish a connection between their respective endpoints. A socket provides the communicating parties with a gateway through which the messages travel. If we talk in terms of the client and server, then the job of the server-side socket will be to listen to the incoming connections, whereas the client-side socket will be responsible for connecting to the server-side socket. This article is intended to make the concept of POSIX socket with C programming much clearer.
Example of Using Posix Socket with C Programming in Linux Mint 20
The example presented to you in this section will demonstrate an interaction between a client and a server. The client and the server are the two main entities of the client/server model in the world of computing. In our example, both the client and the server will be sending and receiving messages to and from each other while making use of the POSIX Socket with C programming in Linux Mint 20. For bringing clarity in the understanding of the code, we have separated the client-side code and the server-side code and will be explaining both of them to you separately below.
The Server-Side Code
For the server-side code, we have simply created an empty document in the Home Directory of our Linux Mint 20 system and named it server.c. In that empty document, you need to write the code snippets shown in the three images below:
The code shown in the images above might seem lengthy, however, let us try to understand it in an extremely easy manner. First of all, we have created a socket and attached it with the desired port number, which in our case is 8080. Then we have written a listen function, which is there to look for all the incoming connections from the clients. Basically, the client manages to connect to the server just because of the presence of this listen function. And once this connection is established, the server is all set to send and receive data to and from the client.
The read and send functions serve the purposes of receiving and sending messages to the clients, respectively. We have already defined a default message in our code that we intend to send our client, and that is “Hello from server”. After sending this message to the client, it will be displayed on the client’s side, whereas a message saying “Hello message sent” will be displayed on the server’s side. This is all about our server-side code.
The Client-Side Code
Now, for the client-side code, again, we have created an empty document in the Home Directory of our Linux Mint 20 system and named it client.c. In that empty document, you need to write the code snippets shown in the two images below:
In the client-side code shown in the images above, we have created a socket in the very same manner as we did for the server-side code. Then, there is a connect function that will attempt to make a connection with the server through the specified port. And once this connection is accepted by the server, the client and server will be all set to send and receive messages to and from each other.
Again, just like the server-side code, the send and read functions are there to send and receive messages from the server, respectively. Also, we have mentioned a default message that we want to send to the server, and that is “Hello from client”. After sending this message to the server, this message will be displayed on the server’s side, whereas a message saying “Hello message sent” will be displayed on the client’s side. And this brings us to the end of the explanation of our client-side code.
Compiling and Running the Client and Server-Side Codes
Obviously, we will be saving both our client-side and server-side files after writing these programs then we will be all set to compile and run these codes. Hence, we will be able to visualize the interaction between our newly created client and server. To achieve this objective, we have to launch two different terminals since we are supposed to run two separate programs. One terminal will be dedicated to running the server-side code, and the other one for the client-side code.
So, for compiling our server-side code, we will execute the following command in the first terminal:
After running this command, if there will be no errors in your server-side code, then nothing will be displayed on the terminal, which will be an indication of a successful compilation.
In the very same manner, we will compile the client-side code with the command shown below by running it in the second terminal:
Once both of the codes are compiled, we will run them one by one. However, we must run the server-side code first since it is supposed to listen to the connection requests. The server-side code can be run with the following command:
After running the server-side code, we can run the client-side code with the command shown below:
Once both the client and server will be up and running, you will witness the outputs shown in the following images on both terminals:
Hopefully, after going through the example shared with you in this article, you will be able to use the POSIX Sockets efficiently for sending and receiving data between a client and server. This example is just a basic demonstration of the Posix sockets with C programming, however, you can even make these programs more complex as per your requirements.