Linux Programming--host byte order and network byte order (15th chapter)

Source: Internet
Author: User
Tags htons

15.2.10 host byte order and network byte order when you run a new version of the server and client on an Intel processor-based Linux machine, you can View network connection Status with the netstat command. It shows that the client/server connection is waiting to close. The connection will close after a period of time, as follows:

You can see the port number of the server and the customer that this connection corresponds to. The Local address column shows the server, while the Foreign address column shows the remote client(even on the same machine, it is still connected over the network). To ensure that all sockets are different, these client ports are generally different from the server listening sockets and are unique on this computer.
However, the local address (server socket) port number that is displayed is 1574, and the selection is 9734. Why is it different?The reason is that the port number and address passed through the socket interface are binary digits, different computers use different byte-order to represent integersFor example, an Intel processor divides a 32-bit integer into 4 contiguous bytes and stores it in memory in byte-order 1-2-3-4, where 1 represents the highest-level byte. IBM The PowerPC processor stores integers in byte 4-3-2-1. If the memory that holds the integers is only copied in bytes, the integer values obtained by the two different computers will be inconsistent.
in order for the different types of computers to agree on the value of a multibyte integer transmitted over the network, a network byte order needs to be defined, the client and server programs must convert their internal integer representations to network byte order before they are passed. They do this by defining a function in the header file Netinet/in.h. These functions are as follows:
#include <netinet/in.h>unsigned long int htonl (unsigned long int hostlong) unsigned short int htons (unsigned short I NT Hostshort); unsigned long int ntohl (unsigned long int netlong); unsigned short int ntohs (unsigned short int netshort);
These functions convert 16-bit integers and 32-bit integers between host byte order and standard network byte orderThe function name is a shorthand for the conversion operation that corresponds to it. For example, "host to Network, Long" (Hton, length integer from host byte order to network byte order conversion) and "host to Net, short" (Htons, A short integer from the host byte order to the network byte order conversion). If the computer itself has the same host byte order and network byte order, the contents of these functions are actually empty operations.
To ensure that the 16-bit port number has the correct byte order, the server and client need these functions to convert the port address. The changes in the new server program server3.c are:
SERVER_ADDRESS.SIN_ADDR.S_ADDR = Htol (inaddr_any); server_address.sin_port = Htons (9734);
You do not need to convert the function call inet_addr ("127.0.0.1") because INET_ADDR has been defined as the result of producing a network byte order. The changes in the new program client3.c are:
Address.sin_port = htons (9734);
The server has also made changes to allow connections to any of the server's network interfaces through Inaddr_any.
When you run Server3 and Client3, you see the following:

using Netstat-a inet will see that the local connection is using the correct port:



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux Programming--host byte order and network byte order (15th chapter)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.