当前位置:网站首页>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;
}
边栏推荐
- [typora installation package] old typera installation package, free version
- Final consistency of MESI cache in CPU -- why does CPU need cache
- How to subcontract uniapp and applet, detailed steps (illustration) # yyds dry goods inventory #
- Jerry's watch listens to the message notification of the target third-party software and pushes the message to the device [article]
- Basic editing specifications and variables of shell script
- The reasons why QT fails to connect to the database and common solutions
- Create template profile
- The latest analysis of hoisting machinery command in 2022 and free examination questions of hoisting machinery command
- MySQL advanced SQL statement (1)
- Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
猜你喜欢

Remember another interview trip to Ali, which ends on three sides

1189. Maximum number of "balloons"

What are the advantages and disadvantages of data center agents?

Magical usage of edge browser (highly recommended by program ape and student party)

Introduction to graphics: graphic painting (I)

Iclr2022 | ontoprotein: protein pre training integrated with gene ontology knowledge

Remember a lazy query error

Idea if a class cannot be found, it will be red

在尋求人類智能AI的過程中,Meta將賭注押向了自監督學習

Use classname to modify style properties
随机推荐
I don't know why it can't run in the project and how to change it
Learn these super practical Google browser skills, girls casually flirt
Small program graduation project based on wechat e-book small program graduation project opening report function reference
[Yugong series] February 2022 attack and defense world advanced question misc-83 (QR easy)
Jerry's watch information type table [chapter]
Yyds dry goods inventory it's not easy to say I love you | use the minimum web API to upload files
Feign implements dynamic URL
Openbionics exoskeleton project introduction | bciduino community finishing
Sequence sorting of basic exercises of test questions
Pytoch residual network RESNET
Life cycle of instance variables, static variables and local variables
Flex flexible layout, box in the middle of the page
15. System limitations and options
Keep an IT training diary 054- opening and closing
13. Time conversion function
Is Shengang securities company as safe as other securities companies
Prose article appreciation - the rain in the warm country has never changed into cold, hard and brilliant flowers. Knowledgeable people think he is monotonous, and he thinks he is unlucky, doesn't he?
From the 18th line to the first line, the new story of the network security industry
Pyrethroid pesticide intermediates - market status and future development trend
What is the intelligent monitoring system of sewage lifting pump station and does it play a big role