当前位置:网站首页>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;
}
边栏推荐
- A. Div. 7
- Customize redistemplate tool class
- C # learning notes: structure of CS documents
- Push technology practice | master these two tuning skills to speed up tidb performance a thousand times!
- Feign implements dynamic URL
- Chain ide -- the infrastructure of the metauniverse
- Human resource management online assignment
- Question d: Haffman coding
- Setting function of Jerry's watch management device [chapter]
- The automatic control system of pump station has powerful functions and diverse application scenarios
猜你喜欢
Final consistency of MESI cache in CPU -- why does CPU need cache
Mysql-15 aggregate function
String & memory function (detailed explanation)
When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
From the 18th line to the first line, the new story of the network security industry
Remember a lazy query error
Solution to the problem that jsp language cannot be recognized in idea
Force buckle day32
Setting function of Jerry's watch management device [chapter]
Feign implements dynamic URL
随机推荐
Yyds dry goods inventory it's not easy to say I love you | use the minimum web API to upload files
Chinese Mitten Crab - current market situation and future development trend
FRP intranet penetration
Day05 branch and loop (II)
Create real-time video chat in unity3d
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
Reading notes - learn to write: what is writing?
Save Private Ryan - map building + voltage dp+deque+ shortest circuit
1189. Maximum number of "balloons"
Huawei cloud micro certification Huawei cloud computing service practice has been stable
Mysql to PostgreSQL real-time data synchronization practice sharing
Is Shengang securities company as safe as other securities companies
How to subcontract uniapp and applet, detailed steps (illustration) # yyds dry goods inventory #
The latest analysis of hoisting machinery command in 2022 and free examination questions of hoisting machinery command
Applet graduation design is based on wechat course appointment registration. Applet graduation design opening report function reference
Maximum likelihood method, likelihood function and log likelihood function
JVM performance tuning and practical basic theory - medium
Chapter 3.4: starrocks data import - Flink connector and CDC second level data synchronization
Huawei cloud micro certification Huawei cloud computing service practice has been stable
Introduction to graphics: graphic painting (I)