C Programming

C: recv Function Usage

Like many socket programming functions, “recv()” is unique and easy to use in C programming. Recv is a method that reads incoming information from link-focused or asynchronous sockets. Before invoking recv utilizing the connection-based protocol, the endpoints, i.e., sockets, should be linked. The ports or sockets should be tied before invoking recv utilizing a link-less protocol. Therefore, within this article today, we will be discussing the use of the “recv()” function in C programming to get the data from a particular IP address. For this, we have been utilizing the Ubuntu 20.04 system. So, let’s start fresh.

Let’s get started with the terminal opening. This has been done with the simple key shortcut “Ctrl+Alt+T” on the Ubuntu 20.04 system desktop screen. Your shell application would be launched within a few moments using the shortcut. The first thing we have to do before moving towards coding is to create a new document of a file of C, i.e., using a C extension. This can be achieved using “touch” instruction within your system shell just opened. It will be created on our system and opened within some built-in editor like text, vim, or nano. To open it within the nano editor, use the “nano” keyword with the file name as shown.

Example 01:

Let’s take a look at our first example to demonstrate the use and working of C’s recv() function in our program. So, we have started including the header libraries, i.e., stdio.h, string.h, sys/types.h, sys/socket.h, netinet/in.h. Here comes the main() and original function of our code from the execution. There is no user-defined function in our code. We have started the main() method with the declaration of integer type variables “s1” and “bcount.” The structure type variable “add” has been constructed with the socket library keyword “sockaddr_in.” This will be declared to add the address of a socket in it. The character type array variable “b” has been declared “512”. The socket() method is castoff to generate a new socket in the variable “s1”.

The socket function takes two arguments, “PF_INET” and “SOCK_STREAM.” The “PF_INET” parameter is referred to as protocol family format for the internet, i.e., TCP, IP. The next parameter, “SOCK_STREAM,” refers to TCP, a link-based protocol. It is used when two endpoints are connected and listening to each other. We have utilized the structure object “add” to set the socket address family for a particular protocol, i.e., AF_INET. This shows the information regarding the socket address.

The same object “add” is used to set the socket port number via the “htons” function. The htons function is a conversion method utilizing the port number, i.e., convert from host byte format to network byte format. The inet_aton() function is here to get the IP address of the socket, convert it to the standard format of network address, and save it to the “sin_addr” built-in using the “add” object. Now the connect() function is used to make the connection between the TCP stream socket “s1” and the outside socket/server via its address, i.e., “add.” Now the “recv” function is utilized to get the data from a connected server and save it to the buffer “b.” This buffer size is obtained from the “sizeof()” function and saved to the variable “bcount. The printf statement will show us the exact bytes of data in our buffer using the bcount variable. The code ends here.

The program has been compiled with the “gcc” compiler first.

After the code execution, we have got the below result showing 1 byte of data is received.

Example 02:

Let’s take another example to receive data from the outer endpoint. So, we have started our code by including some header files in the code. We have defined the size of each chunk that will be received. The timeout_recv() function declaration is here taking 2 arguments.

The main() function starts from the variable “sockdesc” for getting a response. The socket’s address will be stored in the variable “server.” The character type pointer “msg” and an array “server_reply” of size 2000 are declared. We have created a socket of TCP protocol and saved the response in the “sockdesc” variable. If the socket is not created successfully, the printf statement will display that we cannot do that. The server IP address, address family, and the port number have been provided. The connect() function is utilized here to link to the server using the socket. If the connection fails at any level, the linking error message will be presented. If the socket is successfully connected to the given server using IP address and port number, it will display the success message, i.e., connected to a server. The “msg” variable stores the information regarding the server, and the “if” clause is used to check if the data is not transferred successfully. If so, it will show a ‘data sending failed’ message on the shell.

If the data is transferred successfully, the puts functions will display a success message. The timeout_recv() message is called here to check the non-blocking socket timeout. The timeout value 4 has been passed with the “sockdesc” socket variable. The timeout received from this function will be stored in the “tr“cv” variable and displayed on the shell using the printf clause.

The mutable is more or less stated in the timeout_recv() function, i.e., srecv, tsize, start, now, time diff, and array “c.” The “c” array is used to save data in 512 chunks. The fcntl() function is used to make a socket non-blocking. We have got the beginning time using the “gettimeofday” function. The time difference will be calculated. If the socket receives some data, and the time difference calculated is more significant than the timeout passed by the main() function, it will break the loop. Otherwise, it will check if the time difference calculated is 2 times the timeout passed by the main() function. If the condition is satisfied, the “if” statement breaks. The array “c” will be cleared, and if nothing is received, it will sleep for 0.1 seconds. If the data is received, it will calculate the total size and print out the data in chunks while calculating the start time. In last, it will return the total size of data received.

The code got compiled first using the “gcc” built-in command.

After this, the program has been executed with “./a.out” instruction. First of all, the socket got successfully connected to the server, and data was sent successfully. The data received using the “recv” function has been demonstrated in the image underneath.

The current date and time for data received are displayed on the shell. The total size of data received has been displayed as well.


This article has covered all the minor details about using the recv() function of C in socket programming to make it easier for our users. We have tried to cover simple examples to make it possible. Therefore, this article will be a bonus to every C user looking for help in “recv()” function usage.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Page was generated in 0.052035093307495