What is the difference between network byte order and machine order?

xiaoxiao2021-03-05  24

There are two byte sequences: NBO and HBO

Network byte order NBO (Network Byte Order): Use a unified network byte order from a high to low to use a unified network byte order to avoid compatibility issues.

Host byte order (HBO, HOST BYTE ORDER): Different machine HBOs are different, related to CPU design

Computer data store has two bytes priority sequence: high byte priority and low byte priority. The Internet is transmitted on the network in a high byte priority order, so it is necessary to transfer data on the Internet in the Internet to the Internet. The first structural type we have to discuss is: struct sockaddr, this type is used to save Socket information: struct sockaddr {unsigned short sa_family; / * address family, AF_XXX * / CHAR SA_DATA [14]; / ​​* 14 bytes The protocol address * /}; sa_family is generally AF_INET; SA_DATA contains the IP address and port number of the socket. There is also a structural type: strunt sockaddr_in {short int sin_family; / * address family * / unsigned short int sin_port; / * port number * / struct in_addr sin_addr; / * ip address * / unsigned char sin_zero [8]; / * Fill 0 to keep the same size * /} with struct sockaddr; this structure is more convenient. SIN_ZERO (which is used to fill the SockAddr_in structure to the same length as Struct SockAddr) should be placed zero with the Bzero () or MEMSET () function. Poinpectdddr_in's pointer and pointer to SockAddr can be converted to each other, which means that if the parameter type required for a function is SockAddr, you can convert a pointer to the SockAddr_in when the function call is called to point to the socketdr pointer; or the opposite. SIN_FAMILY is usually assigned AF_INET; SIN_PORT and SIN_ADDR should be converted into network byte priority; while SIN_ADDR does not need to be converted. Let's discuss several byte sequential conversion functions: htons () - "Host to NetWork Short"; htonl () - "Host to NetWork Long" () - "NetWork to Host Short"; NTOHL () - - "NetWork to Host Long" Here, h indicates "host", n means "network", and s represents "short", and L means "long". Open the Socket descriptor, establish the binding and establish the connection socket function original to: int Socket (int Domain, int type, int protocol); Domain parameter specifies the type of socket: SOCK_STREAM or SOCK_DGRAM; protocol usually assigns "0". Socket () call returns an integer socket descriptor that you can use it later. Once returns a Socket descriptor via the Socket call, you should associate this socket with a port on you (often call the function when you design the server-side program. You can then listen to the service request at the port And the client generally does not need to call the function).

Bind function original is: int bind (int Sockfd, intectRlen); sockfd is a socket descriptor, my_addr is a pointer to the SockAddr type that contains information such as the native IP address and port number; AddRlen It is set to sizeof (struct sockaddr). Finally, the point to the bind function is that you can automatically get the native IP address and randomly get a port number without the occupied port number: my_addr.sin_port = 0; / * System randomly selects one unused Port number * / my_addr.sin_addr.s_addr = INADDR_Addr; / * Fill in the native IP address * / by setting my_addr.sin_port, the function will automatically select an unidentified port to use. Similarly, by placing my_addr.sin_addr.s_addr is set to INADDR_Any, the system will automatically fill in the native IP address. The bind () function returns 0 when successfully called; return "-1" when an error is encountered and set Errno to the corresponding error number. Also note that when the modulation function is usually, do not set the port number to a value of less than 1024, since 1 to 1024 are reserved port numbers, and you can use any of the port numbers that are not occupied in 1024. The Connect () function is used to create a TCP connection with the remote server, its function prototype: int connection_addr, int addrlen; sockfd is the Sockt descriptor of the destination server; serv_addr is an IP containing the desertive machine IP The address and port number of the port number. Returns -1 when an error is encountered, and the corresponding error code is included in Errno. The client program design does not need to be called bind (), because in this case, you only need to know the IP address of the machine, and which port is connected to the server and the server does not need to care, the kernel will automatically select an unbeatable port for The client is used. Listen () - Monitoring Is Service Request In the server-side program, after the Socket is bundled with a certain port, you need to listen to the port to process the service request arriving. INT Listen (int Sockfd, int Backlog); SockFD is the socket descriptor returned by the socket system call; Backlog specifies the maximum number of requests allowed in the request queue, and the access request will wait for Accept () in the queue (hereinafter) . BACKLOG is limited to the number of requests waiting for the service in the queue, and most system defaults to 20. Returns -1 when Listen encounters an error, Errno is set to the corresponding error code. Therefore, the server-side program typically performs function calls in the following order: socket (); bind (); listen (); / * accept () goes here * / accept () - Connect the service request for the port. This connection request will queue the waiter Accept () when a client attempts to connect with the server listening to the server. The Accept () function returns a new Socket descriptor to be used for this new connection by calling the accept () function.

The server can continue to listen over the previous socket, while the data Send () (send) and RECV () operation can be performed on the new Socket descriptor: int access (int sockfd, void * addr, int *) AddRlen; SockFD is the listened socket descriptor, and addr is usually a pointer to the SockAddr_in variable, which is used to store the information of the host of the connection request service (a host issues the request from a port); AddRTEN usually For a pointer variable pointing value of sizeof (struct socaddr_in). Error returns one -1 and sets the corresponding errno value. Send () and RECV () - Data Transmission These two functions are data transmission on the connection-oriented Socket. Send () function original is: int send (int Len, int flags); SOCKFD is the socket descriptor you want to transfer data, and the MSG is a pointer to the data to send. Len is the length of data in bytes. Flags typically be set to 0 (with the usage of this parameter can be referred to the Man Manual). Char * msg = "Beej Was Here!"; int LEN, BYTES_SENT; ... ... LEN = Strlen (MSG); Bytes_Sent = Send (Sockfd, MSG, Len, 0); ... ... Send ) The function returns the number of bytes actually sent, which may be less than the data you want to send. So you need to measure the return value of Send (). This situation should be processed when the send () return value is mismatched with the LEN. The RECV () function original is: int RECV (int LEN, Unsigned INT FLAGS); SockFD is a socket descriptor that accepts data; BUF is a buffer that stores receiving data; LEN is the length of the buffer. Flags is also set to 0. RECV () returns the number of bytes that is actually received, or when an error occurs, returns the corresponding Errno value. Sendto () and RECVFROM () - Data transfer in the data report in the unconnected datagram socket mode, because the local socket does not establish a connection with the remote machine, the address should be specified when sending data, Sendto ( The function original is: int sendto (int Len, unsigned int flag, const struct socöta); INT tolen; the function is more than two parameters than the send () function, TO means the style The IP address and port number information are often assigned to SizeOf (Struct SockAddr). The SendTo function also returns the actual data byte length or returns -1 when an error occurs. The RECVFROM () function original is: int RECVFROM (int LEN, Unsigned Int Flags, Struct SockAddr * from, INT * FROMLEN); from FROMLEN is a Struct SockAddr type variable, the variable holds the IP of the source Address and port number. Fromlen is always set to SIZEOF (Struct SockAddr). When Recvfrom () returns, the fromLOM contains the number of data bytes actually stored in the FROM.

The RECVFROM () function returns the number of bytes received or returns -1 when an error occurs, and the corresponding errno is copied. It should be noted that when you call the connect () function for the duplicate Socket, you can use Send () and RECV () to perform data transfer, but the socket is still a datagram, and uses the UDP of the transport layer. service. However, when sending or receiving a data report, the kernel will automatically add the purpose and source address information. Close () and ShutDown () - End Data Transfer When all data operations are completed, you can call the Close () function to release the socket, stop any data operation on the socket: Close (SockFD); you also You can call the shutdown () function to close the socket. This function allows you to only stop data transmission in a certain direction, while the data transfer in one direction continues. If you can turn off a Socket write operation, you can continue to accept data on the socket until all data is read. INT Shutdown (int suckfd, int how); SockFD's meaning is obvious, and parameter how can be set to the following: · 0 ------- Do not allow continued reception data · 1 ------- Allows to send data · 2 ------- Do not allow continued transmission and reception data, all call close () shutdown returns 0 when the operation is successful, return -1 (parallel corresponding errno) when an error occurs . DNS - Domain Name Service Related Functions Since the IP address is difficult to remember and read, in order to read and write memories, people often use domain names to represent the host, which requires the conversion of domain names and IP addresses. The function gethostByname () is to complete this conversion, the function original is: struct hostent * gethostbyname (const char * name); function returns a structure type called Hosten, which is defined as follows: struct hostent {char * h_name; / * The official domain name of the host * / char ** h_aliases; / * An address type returned by the host other name * / int h_addrtype; / * ending with NULL, an AF-INET * / INT H_LENGTH; / * address in the Internet environment Byte length * / char ** h_addr_list; / * An array ends with 0, including all addresses of the host * /}; #define h_addr h_addr_list [0] / * The first in H-Addr-list Address * / 2, convert the host's unsigned long value to network byte order (32 digits): Why do you want to do this? Data is stored in different bytes using different computers. Therefore, any IP address and port number from the Winsock function to the IP address and port number and the IP address and port number passing to the Winsock function are organized according to the network sequence. U_LONG HTONL (U_LONG HOSTLONG); Example: HTONL (0) = 0 htonl (80) = 1342177280

3. Transfer the Unsigned long number from the network byte sequence sequence, which is the inverse function of the above function. U_long ntohl; example: ntohl (0) = 0 ntohl (1342177280) = 80

4. Convert the host's unsigned short value to network byte sequence (16 bits): Cause 2: u_short htons (u_short hostshort); Example: htonl (0) = 0 HTONL (80) = 204805, the number of unsigned shorts The network byte sequential conversion bit host byte order is the inverse function of the above function. U_SHORT NTOHS (U_SHORT NETSHORT); Example: NTOHS (0) = 0 NTOHSL (20480) = 80

转载请注明原文地址:https://www.9cbs.com/read-34789.html

New Post(0)