当前位置:网站首页>Network byte order
Network byte order
2022-07-04 02:10:00 【Soy sauce;】
Network byte order description
TCP/IP Provisions of the agreement , Network data stream should adopt big end byte order , That is, low address, high byte . For example, in the previous section UDP Segment format , Address 0-1 yes 16 Bit source port number , If the port number is 1000(0x3e8), Address 0 yes 0x03, Address 1 yes 0xe8, That is, the first 0x03, Reissue 0xe8, this 16 The bit in the buffer of the sending host should also be a low address memory 0x03, Highland site storage 0xe8. however , If the sending host is small endian , this 16 Bits are interpreted as 0xe803, instead of 1000. therefore , Send the host to 1000 Byte order conversion is required before filling in the send buffer . similarly , If the receiving host is in small end byte order , Receive 16 The source port number of bit also needs to be converted into byte order . If the host is big endian , No conversion is required for sending and receiving . Empathy ,32 Bit IP The address should also consider the network byte order and host byte order .
In order to make the network program portable , Make the same C The code can run normally after compiling on both big end and small end computers , You can call the following library functions to do network byte order and host byte order conversion .
Network big end , Small end of host
Network and host byte order conversion function – Data are all addresses
#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong); Host to network , Long
uint16_t htons(uint16_t hostshort); Network to host , short
uint32_t ntohl(uint32_t netlong); Network to host , Long
uint16_t ntohs(uint16_t netshort); Network to host , short
h Express host,n Express network,l Express 32 Bit long integers ,s Express 16 Bit short integer .
If the address of the host is small endian , These functions do the corresponding large and small conversion of parameters, and then return , If the host is big endian , These functions don't convert , Return the parameters as they are .
Dot decimal string conversion function
#include <arpa/inet.h>
int inet_pton(int af, const char *src, void *dst);// Hexadecimal string to big end 
const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);// Big end to hexadecimal string


Support IPv4 and IPv6
Reentrant function
among inet_pton and inet_ntop Not only can it be converted IPv4 Of in_addr, You can also convert IPv6 Of in6_addr.
So the function interface is void *addrptr.
Code

#include <stdio.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
char buf[]="192.168.1.4";
unsigned int num=0;
inet_pton(AF_INET,buf,&num);
unsigned char * p = (unsigned char *)#
printf("%d %d %d %d\n",*p,*(p+1),*(p+2),*(p+3));
char ip[16]="";
printf("%s\n",inet_ntop(AF_INET,&num,ip,16));
return 0;
}
边栏推荐
- mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
- The difference between lambda expressions and anonymous inner classes
- 2022 electrician (elementary) examination question bank and electrician (elementary) simulation examination question bank
- Sword finger offer 14- I. cut rope
- Format character%* s
- Applet graduation project based on wechat selection voting applet graduation project opening report function reference
- Feign implements dynamic URL
- Mysql to PostgreSQL real-time data synchronization practice sharing
- Question C: Huffman tree
- The reasons why QT fails to connect to the database and common solutions
猜你喜欢

Maximum entropy model

TP5 automatic registration hook mechanism hook extension, with a complete case

Force buckle day32

How programmers find girlfriends through blind dates

Comment la transformation numérique du crédit d'information de la Chine passe - t - elle du ciel au bout des doigts?

Take you to master the formatter of visual studio code

Chapter 3.4: starrocks data import - Flink connector and CDC second level data synchronization

SQL statement

Chain ide -- the infrastructure of the metauniverse

中電資訊-信貸業務數字化轉型如何從星空到指尖?
随机推荐
What is the student party's Bluetooth headset recommendation? Student party easy to use Bluetooth headset recommended
Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
The requests module uses
17. File i/o buffer
1189. Maximum number of "balloons"
Yyds dry goods inventory override and virtual of classes in C
Data collection and summary
Take you to master the formatter of visual studio code
14. Process time
Create template profile
Special copy UML notes
Basic editing specifications and variables of shell script
Small program graduation project based on wechat video broadcast small program graduation project opening report function reference
When the watch system of Jerry's is abnormal, it is used to restore the system [chapter]
Example 072 calculation of salary it is known that the base salary of an employee of a company is 500 yuan. The amount of software sold by the employee and the Commission method are as follows: Sales
Difference between value and placeholder
Portable two-way radio equipment - current market situation and future development trend
Three layer switching ①
The reasons why QT fails to connect to the database and common solutions
mysql使用視圖報錯,EXPLAIN/SHOW can not be issued; lacking privileges for underlying table