Within Socket programming, we must have two nodes, i.e., client and a server. An endpoint for transmission is known as a POSIX Socket or just a Socket. For sender and receiver, A and B, to interact with one another, both these sides must first create a link across their particular endpoints. Another socket (endpoint) seeks out the first socket to generate a link, whereas the first socket responds on a certain port upon an IP address. Whereas the client approaches the server, the server generates the listener socket. In this guide, we will be explaining the use of the Socket function from the POSIX library of C while using the Ubuntu 20.04 Linux operating system.
Writing client and server code is necessary for socket programming. A single node or server can have several clients connected to it. The necessary header files to employ while programming sockets are included in the first eight lines. By setting the value to 8080, we define the PORT global variable. If Port 8080 is not available, we can swap it to any other port that is.
We declare a few integer-type variables in the main method that we’ll use later. These variables include those for the server file descriptor (server_file_desc), the new socket (new_socket), and the value or message read from the client (value_Read). The addresses for the internet address family are stored in a structure of type sockaddr, which is declared in the line after.
Later, we defined option=1, address Length=sizeof(socket_Address), and buffer, which were initially set to 0 and were used for messages retrieved from clients of a size of 1024 bytes. The message variable, which stores the message sent by the server to the clients connected to it, comes next. In the next line, we are creating a socket and passing it AF_INET (The address family known as AF INET is used to specify the kinds of addresses that your socket can connect with (in this case, IP (Internet Protocol) v4/v6 addresses).
TCP is a transmission communication protocol that is a connection-based protocol. Once the connection is established between two parties (Client-Server), communication starts until the connection is terminated or aborted by either of the two parties or by some type of network connection error. Here is the snap of the Server Side Code written in the server.c file.
Now we are forcefully attaching the socket to port 8080. After that, we initialize the socket family, address, and port. After that, we bind the socket address to port 8080. Now the server is ready to listen to any connection from the client. So we print a message before the listening method. When any client connects to the server using this port, the server will accept the client connection.
If the client and server are connected, the server will read the message from the client using a buffer and display it on the terminal screen. Similar as far as how clients get messages from servers, send methods to transmit a message ,and socket information to clients. The server then displays a success message before cutting off communication with the associated socket. The server also closed the listening socket port on the server.
The below image shows the Client.c File Code Snippet:
The client connects to the server using a socket using the connect() method, and we also passed the server address. The client and server can communicate once the connection has been established effectively. Display a failure message on the terminal screen if the server is down or the client is unable to connect for any other reason. Following a successful connection, the client will use a socket to send a message to the server, read the message from the server using a buffer into the value Read variable, and display the server’s reply on the screen. The client then shuts off communication with the linked socket.
We have compiled the Server.c and Client.c files using the GCC Compiler, and we have saved the results in the Server.c and Client.c folders. The commands to compile the Server and Client Files are listed below.
We needed two distinct terminals, one for the server and one for the client, to see the communication between them. Press Ctrl+SHIFT+N to launch a new terminal in Ubuntu. One thing you must keep in mind is that the server must be active and waiting for a connection from the client. You will receive the error “Connection with the Server failed” if the server is offline and not connected for any reason while the client wants to connect to the server. The snap below shows this error:
When we run the server output file, the server will wait for the client to connect before the two of them may communicate. Simply type ./Server on the freshly opened terminal to start the server. Here is a screenshot showing that the server is up and running and ready to accept connections from clients:
It’s time to start the client right now. To initiate communication between the client and server, type “/Client” on a new terminal. The server then responds and sends a message, which the client reads, stores in a message variable, and displays using a printf statement. The client’s message was displayed on the terminal screen below.
When a client attaches to a server, the server accepts a message to verify the connection. The client’s message is therefore shown on the screen below. Following that, the server will notify the client and display a success message. Following this successful communication, the server terminates or aborts the connection with the client.
We are done with enough explanations and demonstrations of socket programming on the client and server-side using the POSIX library of the C language. We have briefly explained both the code sides of socket programming and hope that you will learn from the examples.