当前位置:网站首页>socket inet_ pton() inet_ Ntop() function (a new network address translation function, which converts the expression format and numerical format to each other. The old ones are inet_aton(), INET_ ntoa
socket inet_ pton() inet_ Ntop() function (a new network address translation function, which converts the expression format and numerical format to each other. The old ones are inet_aton(), INET_ ntoa
2022-07-04 07:11:00 【Dontla】
New network address translation function :
Remove the buffer from CP At the beginning Internet The representation format of the number is converted to binary network format , And the interface type AF The results are stored from BUF In the start buffer .
The function of this code is similar , What's the difference ?
serv_addr.sin_addr.s_addr = inet_addr("192.168.1.116");
take Internet Host address from CP The numbers and dot symbols in are converted into binary data in network byte order .
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). The expression format of address (presentation) Usually ASCII character string , The numerical format (numeric) Is the 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.
Reference article 1:inet_pton() and inet_ntop() Function details
Reference article 2:IP Address translation function :inet_pton
边栏推荐
- 【GF(q)+LDPC】基于二值图GF(q)域的规则LDPC编译码设计与matlab仿真
- selenium IDE插件下载安装使用教程
- [FreeRTOS] FreeRTOS learning notes (7) - handwritten FreeRTOS two-way linked list / source code analysis
- Tar source code analysis 6
- the input device is not a TTY. If you are using mintty, try prefixing the command with ‘winpty‘
- 2022, peut - être la meilleure année économique de la prochaine décennie, avez - vous obtenu votre diplôme en 2022? Comment est - ce prévu après la remise des diplômes?
- [FPGA tutorial case 7] design and implementation of counter based on Verilog
- SQL foundation 9 [grouping data]
- 抽奖系统测试报告
- 关于IDEA如何设置快捷键集
猜你喜欢
Experience installing VMware esxi 6.7 under VMware Workstation 16
Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology
Computer connects raspberry pie remotely through putty
Cervical vertebra, beriberi
Boosting the Performance of Video Compression Artifact Reduction with Reference Frame Proposals and
Introduction to spark core components
Centos8 install mysql 7 unable to start up
【森城市】GIS数据漫谈(一)
How to share the source code anti disclosure scheme
selenium IDE插件下载安装使用教程
随机推荐
Check and display one column in the known table column
果果带你写链表,小学生看了都说好
【FPGA教程案例8】基于verilog的分频器设计与实现
The final week, I split
MySQL 45 lecture learning notes (VI) global lock
输入年份、月份,确定天数
响应式——媒体查询
selenium IDE插件下载安装使用教程
CMS source code of multi wechat management system developed based on thinkphp6, with one click curd and other functions
How does the recv of TCP socket receive messages of specified length?
Campus network problems
抽奖系统测试报告
[untitled] notice on holding "2022 traditional fermented food and modern brewing technology"
MySQL relearn 2- Alibaba cloud server CentOS installation mysql8.0
Crawler (III) crawling house prices in Tianjin
The number of patent applications in China has again surpassed that of the United States and Japan, ranking first in the world for 11 consecutive years
电子协会 C语言 1级 34 、分段函数
同一个job有两个source就报其中一个数据库找不到,有大佬回答下吗
[MySQL transaction]
Introduction to deep learning Ann neural network parameter optimization problem (SGD, momentum, adagrad, rmsprop, Adam)