C Programming

Send 2 C Function

“To send a message from one socket to another channel, we can utilize the system send(), sendto(), and sendmsg() functions of the C language in the Kali Linux system. We can invoke the send() function when the socket is connected (so that the intended recipient is known). The existence of flags is the only differentiator between send() and write(2) functions. The send() function is equivalent to write(2) with a flags parameter of zero. Moreover, the next call is equivalent to wq1

send (socket_file_descriptor, buffer, length, flags);

sendto (socket_file_descriptor, buffer, length, flags, NULL, 0);

The sending socket’s file descriptor is contained in the argument socket_file_descriptor. The message is located in a buffer and has a length of length for send() and sendto(). We need a server and client terminals to understand the functionality of the send() function practically. For this purpose, we have to write the code for the server as well as the client. Utilizing the VIM editor, create a server.c programming file. This is the command to open a coding file in VIM Editor.”

After including the necessary header files in the first five lines of the server.c file, we define a PORT variable and set its value to 8000. Following the launch of the main function, we declare three integer-type variables called server fd (server file descriptor), a new socket, and a message read in the first line. Declare the address variable of the sockaddr type after that.

Next, we define the value of a variable option to be 1. Utilizing the size_of function, which is equal to address, define the address length variable. Set the size of the character-type array variable buffer to 1024, and define the values to 0. A character-type variable message is then defined and given the value of “Hi, I am Server. How can I help you?”. The types of addresses that your socket can communicate with are designated by the address family AF_INET (in this case, Internet Protocol v4 addresses).

In a conditional statement, assign the server file descriptor and check to see if the assignment causes an error. If it fails, show the error message “Socket Failed” and leave the main function. Using the setsockopt() function, an application program can manage how a socket behaves. Because port 8080 is unavailable, we are forcibly attaching the socket to port 8000 in this conditional expression. We purposefully bind the server socket to port 8080 in the second conditional line. If the binding process did not go as planned, use the perror method to display an error message and the exit function to leave the main function. Verify whether the server is listening for the client request in the third conditional expression.

We accept the server connection and set the new socket value in the fourth conditional statement, indicating that the server is receiving the client request. The message sent by the client using the socket and buffer should now be read. The client message will then be shown on the terminal screen and sent to the client indicated at the beginning of the main function. Close the socket and shut down the channel using the shutdown mechanism after a successful communication has taken place while passing the server file descriptor and SHUT RDWR command.

Close the VIM editor and compile the code by using the GCC compiler. Save the output or object file in the server file:

It is now time to test whether the server code is running or not after it has completed compilation. Use the./server script inside the terminal and the Enter key to do this. When the cursor blinks, the server is active and waiting for a request from the client:

Now we have to write the client code using the VIM editor and save it in the client.c file.

The code for the client is displayed on the screen below. It includes the header files in the first few lines and sets the PORT used for communication between server and client to 8000. Create three integer-type variables and set the value of the socks to 0 inside the main function. Next, make a variable with the sockaddr in type. Set a message variable with the value “Greetings from Client!” and save it.

Now use a conditional statement to create the socket and save the value to the socket variable. The IPV4 or IPV6 addresses are then converted from text to binary. We make use of the server’s default 127.0.0.1 IP address. We employ the inet_pton function for this reason. Next, we create the code to use the connect function to connect to the socket and define the client file descriptor. The server address was supplied to it. We are now prepared to use the send function to deliver the message to the server.

The socket, message, message length, and flag—which is set to 0—are all included in the send method. Display the on the client terminal when the message has been sent. Then read the server’s answer and display it on the screen. Close the file descriptor after a successful communication.

Now compile the code as given in the below snippet.

You must keep in mind that you must run the server before running the client because if the server isn’t responding, the client won’t connect, and an error message will appear on the screen. In the event of a communication breakdown, this is the output.

Here is the screenshot of successful communication on the Server-side:

On the client-side:

Conclusion

Within this article, we have demonstrated the use of send() function of C in the Kali Linux program. Be aware that we cannot interact on the same terminal, so we must open two, one for the server and another one for the client. This example can be adjusted as per user needs.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.