当前位置:网站首页>inet_ Pton () and INET_ Detailed explanation of ntop() function
inet_ Pton () and INET_ Detailed explanation of ntop() function
2020-11-09 11:32:00 【Pile】
1. hold ip The address is converted into a binary value for network transmission
int inet_aton(const char *cp, struct in_addr *inp);
inet_aton() transformation Network host address ip( Such as 192.168.1.10) by Binary values , And stored in struct in_addr In structure , That's the second parameter *inp, Function returns non 0 Express cp The host is efficient , return 0 Indicates that the host address is invalid .( This conversion cannot be used for network transmission , You also need to call htons or htonl Function to convert host byte order to network byte order )
in_addr_t inet_addr(const char *cp);
inet_addr Function conversion Network host address ( Such as 192.168.1.10) by Network byte order binary value , If parameters char *cp Invalid , The function returns -1(INADDR_NONE), This function is dealing with address 255.255.255.255 When you go back to -1,255.255.255.255 Is a valid address , however inet_addr Unable to deal with ;
2. Convert the binary value of network transmission into dot decimal ip Address
char *inet_ntoa(struct in_addr in);
inet_ntoa function Translate network byte sort address by The standard ASCII Addresses separated by dots , This function returns the address of the string pointing to the point separated ( Such as 192.168.1.10) The pointer to , Space allocated for the static string , This means that when the function is called the second time , The last call will be overridden ( Covering ), So if you need to save the string, you can copy it and manage it yourself !
How do we output a dot decimal IP Well ? Let's take a look at the following program :
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
int main()
{
struct in_addr addr1,addr2;
ulong l1,l2;
l1= inet_addr("192.168.0.74");
l2 = inet_addr("211.100.21.179");
memcpy(&addr1, &l1, 4);
memcpy(&addr2, &l2, 4);
printf("%s : %s\n", inet_ntoa(addr1), inet_ntoa(addr2)); // Pay attention to the result of this sentence
printf("%s\n", inet_ntoa(addr1));
printf("%s\n", inet_ntoa(addr2));
return 0;
}
The actual operation results are as follows :
192.168.0.74 : 192.168.0.74 // You can see it here ,printf Inside inet_ntoa Only run once .
192.168.0.74
211.100.21.179
inet_ntoa Return to one char *, And this char * The space is in inet_ntoa It's statically allocated , therefore inet_ntoa Subsequent calls will override the previous call . The first sentence printf The result can only show that in printf Variable parameters in it It's evaluated from right to left Of , That's it .
3. New network address translation function inet_pton and inet_ntop
These two functions follow IPv6 Functions that appear , about IPv4 Address and IPv6 All addresses apply , Function p and n Each represents the expression (presentation) And numbers (numeric). Address is usually expressed in the form of ASCII character string , The numeric format is a binary value stored in the socket address structure .
#include <arpa/inet.h>
int inet_pton(int family, const char *strptr, void *addrptr); // Will be dotted decimal ip The address is converted into a numeric format for network transmission
Return value : If successful 1, If the input is not a valid expression 0, In case of error -1
const char * inet_ntop(int family, const void *addrptr, char *strptr, size_t len); // Convert numerical format to dot decimal ip Address format
Return value : If successful, a pointer to the structure , In case of error NULL
(1) Of these two functions family The parameter can be either AF_INET(ipv4) It can also be AF_INET6(ipv6). If , Take an unsupported address family as family Parameters , Both functions return an error , And will errno Set as EAFNOSUPPORT.
(2) The first function attempts to convert from strptr The string that the pointer points to , And pass addrptr The pointer holds the binary result , If successful, the return value is 1, Otherwise, if specified family The input string is not a valid expression format , So the return value is zero 0.
(3)inet_ntop Do the opposite conversion , From the numerical format (addrptr) Convert to expression (strptr).inet_ntop Functional strptr Parameter cannot be a null pointer . The caller must allocate memory for the target storage unit and specify its size , When the call succeeds , This pointer is the return value of the function .len Parameter is the size of the target storage unit , To prevent the function from overflowing its caller's buffer . If len Too small , Not enough to hold expression results , Then return a null pointer , Juxtaposition as errno by ENOSPC.
4. Example
inet_pton(AF_INET, ip, &foo.sin_addr); // Instead of foo.sin_addr.addr=inet_addr(ip);
char str[INET_ADDRSTRLEN];
char *ptr = inet_ntop(AF_INET,&foo.sin_addr, str, sizeof(str)); // Instead of ptr = inet_ntoa(foo.sin_addr)
版权声明
本文为[Pile]所创,转载请带上原文链接,感谢
边栏推荐
- Stack & queue (go) of data structure and algorithm series
- Rainbow sorting | Dutch flag problem
- AI应届生年薪涨到40万了,你现在转行还来得及!
- Method of creating flat panel simulator by Android studio
- Impact of libssl on CentOS login
- 基于synchronized锁的深度解析
- 手写Koa.js源码
- Depth analysis based on synchronized lock
- 捕获冒泡?难道浏览器是鱼吗?
- 开源 | HMGNN:异构小图神经网络及其在拉新裂变风控场景的应用
猜你喜欢
医疗项目管理的三种实用技巧
微信视频号播主排行榜2020年10月
EFF 认为 RIAA 正在“滥用 DMCA”来关闭 YouTube-DL
Stack & queue (go) of data structure and algorithm series
Depth analysis based on synchronized lock
Android权限大全
5年程序员面试,常见面试问题解析
Handwriting Koa.js Source code
Open source ERP recruitment
The whole process of building a fully distributed cluster
随机推荐
配置交换机Trunk接口流量本地优先转发(集群/堆叠)
从编码、网络传输、架构设计揭秘腾讯云高质量、高可用实时音视频技术实践...
JT-day09
微信圈子
理解Task和和async await
Handwriting Koa.js Source code
解决python调用 ffmpeg时 ‘ffmpeg‘ 不是内部或外部命令,也不是可运行的程序
如何保证消息不被重复消费?(如何保证消息消费的幂等性)
Composition - API
Interview summary on November 7, 2020 (interview 12K)
Mapstructure detoxifies object mapping
ThinkPHP框架执行流程源码解析
Fedora 33 Workstation 的新功能
日志分析工具 - GoAccess
git 删除iml文件
Mac terminal oh my Zsh + solarized configuration
SHOW PROFILE分析SQL语句性能开销
Elasticsearch原理解析与性能调优
Android权限大全
共创爆款休闲游戏 “2020 Ohayoo游戏开发者沙龙”北京站报名开启