Example 1:
For creating a C code, you must have the GCC compiler mounted on your Kali Linux system. Within the Kali Linux shell, you need to create a new C file named “htons.c”. You can open this file with the “nano” instruction in a single instruction listed in the image.
Now that the C file is already launched in the Nano editor, we are going to add some C script to elaborate the use of “htons” in C. Every C code starts with some of its headers included at the top so that we can easily use the related functions and variables in the code. Before that, we need to include the main headers required and necessary for the successful transmission and use of input and output data into the script. Therefore, we have been casting off the “include” keyword to add the standard header “stdio.h”, “stdlib.h”, and “unistd.h” in the code.
After this, we need to add some system headers to enable socket programming in C programming. Therefore, we are using the “sys/types.h”, “sys/socket.h”, and “netinet/in.h” header in the code. To utilize the use of htons() functions in the C code, we need to use the “arpa/inet.h” header here as well. Now, it’s time to define port 8082 with the CONNECT_PORT keyword that is needed to connect the users in socket programming. The overall work would be done within the main() function of C programming i.e., driver code of C. This function has been started with the use of the struct keyword to define the structure “sockadd_in” with a variable name “address” to add the socket address in this variable.
Now, the socket address structure object “address” has been used to utilize the sin.port variable that has been calling the htons() function with the CONNECT_PORT variable i.e., having a port number in it. The htons() function would be enabling the use of various byte order standards.
Now, it’s time to compile the C file named “htons.c” after saving the above code. To compile it, you need to use the “gcc” keyword with the file name “htons.c”, -o option and the object file name to be created i.e., “htons.out”.
Now, when you run the object file created by the gcc instruction, it will enable the use of standard byte orders in our system.
Example 2:
It’s time to create another example of C to utilize the htonl function along with the htons function in the code. For this, we have been creating another C file named “server.c” and opening it with the standard GNU Nano editor as shown in the image attached.
After opening the C file, we added the very same headers in the top lines as we did in the first example. But, we have also added some new headers as well: “time.h” to use the time function, the “errno.h” is to get the error number, and the “string.h” header has been cast off to use the string variables and functions in the code. This code only contains the main() driver method.
Two descriptors of integer types have been declared to 0. One is to listen to the file and the other to make a socket connection. The structure “socket_address” has been specified using the sockaddr_in built-in structure of socket programming that will store the address of a socket. A character array named “messageBuffer” of size 1025 is defined to store any message that will be converted to another byte order. The time_t built-in time variable is used to define the “ticks” variable to count the total ticks of a clock. The listen_file_descriptor() will be holding the return value from the socket() function or the socket initialization through AF_INET and SOCK_STREAM options.
The memset() function is used here to set the first count bytes of a server_address and messageBuffer variable to “0” probably while using the sizeoof() function. Using the server_address built-in variable, we have defined the socket family as “AF_INET”, and the socket input address as “INADDR_ANY” using the htonl() function of C by calling the s_addr built-in variable. In the same way, the server_address variable has been utilized to call the sin_port object and store the port number 8081 in it by using the htons() function of C.
Now, the bind() function of socket programming in C has been used to bind the socket with the address specified using the listen_file_descriptor variable and server_address variable in it. The listen function of the C program has been called here to listen to the socket at number 10. The while loop has been initialized here to create a connection between the socket and time while using the accept() function. This process has been getting the data in time byte data sequence and converting it to standard data byte order that can be used by the socket and local machine. In the end, the connection closed without any issue.
After executing and compiling this program, we have generated its object file.
On running its object file, the socket has started to listen to the ticks of time for each second and convert it to standard byte order.
Conclusion
This article is about the use of Network Byte Order standard functions to convert some different data coming from the web to standard network byte sequences. For this, we have utilized two examples using the htons() and htonl() functions.