Purpose and Components of the SockAddr_In Structure in the C Programming Language
We have briefly stated the purpose of the “sockaddr_in” structure of the C programming language in the introduction of this article. Now, we will try to acquire more about it by discussing its various components. The three main components of the “sockaddr_in” structure of the C programming language that we are also going to use in our example are discussed below:
- sin_family: This component refers to an address family which in most of the cases is set to “AF_INET”.
- sin_addr: It represents a 32-bit IP address.
- sin_port: It refers to a 16-bit port number on which the server will listen to the connection requests by the clients.
Once you populate all the components of the “sockaddr_in” structure, you can easily use the created socket for interacting with the desired clients.
The Program to Demonstrate the Usage of the SockAddr_In Structure in the C Programming Language:
For demonstrating to you the usage of the “sockaddr_in” structure in the C programming language, we have written a simple program for the basic interaction of a client and a server. Both the server and the client-side codes will be discussed separately below:
The Server-Side Code:
For the server-side code of this example, we have first included all the required libraries or header files and all of them are shown in the following image:
Then, the code that we have written is shown in the images below:
After including the libraries, we have our “main()” function within which we have declared three integer variables named “server_fd”, new_socket, and “valread”. We are going to utilize all three of these variables later in our code. Then, we have created an object of the “sockaddr_in” structure named “address”. Then, we have created another integer variable “opt” and assigned to it the value “1”. After that, we have created an integer variable named “addrlen” and have assigned to it the size of the “address” object. We have created a character type buffer for holding the messages sent by the client. Then, we have created a character type pointer named “hello” and have assigned to it a sample message.
We also have an “if” statement for handling the socket creation failure error. We have another “if” statement for catering to all other types of errors associated with sockets. Then, by making use of the “address” object, we have populated the components of the “sockaddr_in” structure i.e., sin_family, sin_addr.s_addr, and sin_port with suitable values. After that, we have bound the newly created socket to the provided address by making use of another “if” statement. Then, we have checked whether the server renders any errors while listening or not by using yet another “if” statement.
After that, we have an “if” block for making the server accept the connection request from whichever client wishes to connect to it and send and receive messages. Then, we have used the “valread” variable to read the message sent by the client in the “buffer” variable. Then, we have simply printed the value of the “buffer” variable on the terminal. We have used the “send” command for sending the message that we had assigned to the “hello” string earlier to the client. Finally, we wanted to print a confirmation message on the server-side terminal.
The Client-Side Code:
For the client-side program, we implemented the code shown in the following images:
We have first included all the required libraries and header files followed by our “main()” function in which we have created two integer variables named “sock” and “valread”. Then, we have created an object of the structure “sockaddr_in” named “serv_addr”. After that, we have created a “hello” character pointer and assigned to it the message that we want to send to the server. Then, we have a character type buffer to hold the message received by the server. We also have an “if” statement to check if there is a socket creation error or not.
By using the “serv_addr” object, we have populated the components of the “sockaddr_in” structure in the same way as we populated them for the server side of the code. After that, we have used an “if” statement to check if the provided IP address is valid or not. Then, we have another “if” statement for connecting the client with the server. We have sent the message contained in the “hello” string to the server by using the “send” command. We have printed a confirmation message on the client-side terminal. Finally, we have read and printed the message sent by the server on the client-side terminal.
Compilation and Execution of the C Code:
For compiling both of our C scripts, we have first launched two different terminal windows (one for the client and one for the server) on Linux Mint 20 and have used the commands shown below on each of these terminals:
After a successful compilation of both of our C scripts, we have to execute the server side first by running the following command so that it gets into the listening mode and any client can easily connect with it.
After that, we need to execute the client-side code by running the command shown below:
As soon as you will hit the Enter key after typing the above-mentioned command on the terminal, you will receive the following messages on the client-side terminal:
Moreover, if you will look at the server-side terminal now, then you will be able to see the messages shown in the image below:
This is how you can use the “sockaddr_in” structure in the C programming language for creating a basic program for the communication between a client and a server.
In this article, we aimed to introduce you to the “sockaddr_in” structure of the C programming language. We first discussed the purpose of this structure briefly followed by the description of its main components. After that, we demonstrated to you a full-fledged example in C designed for the interaction between a client and a server that makes use of the “sockadd_in” structure. Not only we shared the code for these C scripts, but we also taught you the right sequence of executing these scripts i.e., the server-side code should be executed first followed by the client-side code. This is completed so that the client can easily connect to the server as soon as it is executed. You can easily learn the purpose and usage of the “sockaddr_in” structure of the C programming language after going through the example that we have demonstrated in this article.