inet_ntop函数和inet_pton函数
#include<arpa/inet.h>
int inet_pton(int family, const char *strptr, void *addrptr);返回:若成功则为1,若输入不是有效的表达格式则为0,若出错则为-1
const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t len);返回:若成功则为指向结果的指针, 若出错则为NULL
这两个函数的family参数既可以是AF_INET,也可以是AF_INET6。如果以不被支持的地址族作为family的参数,这两个函数就都返回一个错误,并将errno置为EAFNOSUPPORT。
第一个函数尝试转换由strptr指针所指的字符串,并通过addrptr指针存放二进制结果。若成功则返回1,否则如果对所指定的family而言输入的字符串不是有效的表达式,那么值为0。
inet_ntop进行相反的转换,从数值格式(addrptr)转换到表达格式(strptr)。len参数是目标存储单元的大小,以免该函数溢出其调用者的缓冲区。为有助于指定这个大小,在
INET_NTOP(3) Linux Programmer's Manual INET_NTOP(3)NAMEinet_ntop - convert IPv4 and IPv6 addresses from binary to text formSYNOPSIS#include <arpa/inet.h>const char *inet_ntop(int af, const void *src,char *dst, socklen_t size);DESCRIPTIONThis function converts the network address structure src in the afaddress family into a character string. The resulting string is copiedto the buffer pointed to by dst, which must be a non-null pointer. Thecaller specifies the number of bytes available in this buffer in theargument size.inet_ntop() extends the inet_ntoa(3) function to support multipleaddress families, inet_ntoa(3) is now considered to be deprecated infavor of inet_ntop(). The following address families are currentlysupported:AF_INETsrc points to a struct in_addr (in network byte order) which isconverted to an IPv4 network address in the dotted-decimal for‐mat, "ddd.ddd.ddd.ddd". The buffer dst must be at leastINET_ADDRSTRLEN bytes long.AF_INET6src points to a struct in6_addr (in network byte order) which isconverted to a representation of this address in the most appro‐priate IPv6 network address format for this address. The bufferdst must be at least INET6_ADDRSTRLEN bytes long.RETURN VALUEOn success, inet_ntop() returns a non-null pointer to dst. NULL isreturned if there was an error, with errno set to indicate the error.ERRORSEAFNOSUPPORTaf was not a valid address family.ENOSPC The converted address string would exceed the size given bysize.ATTRIBUTESFor an explanation of the terms used in this section, seeattributes(7).┌────────────┬───────────────┬────────────────┐│Interface │ Attribute │ Value │├────────────┼───────────────┼────────────────┤│inet_ntop() │ Thread safety │ MT-Safe locale │└────────────┴───────────────┴────────────────┘CONFORMING TOPOSIX.1-2001, POSIX.1-2008. Note that RFC 2553 defines a prototypewhere the last argument size is of type size_t. Many systems followRFC 2553. Glibc 2.0 and 2.1 have size_t, but 2.2 and later havesocklen_t.BUGSAF_INET6 converts IPv4-mapped IPv6 addresses into an IPv6 format.EXAMPLESee inet_pton(3).SEE ALSOgetnameinfo(3), inet(3), inet_pton(3)COLOPHONThis page is part of release 4.04 of the Linux man-pages project. Adescription of the project, information about reporting bugs, and thelatest version of this page, can be found athttp://www.kernel.org/doc/man-pages/.Linux 2015-08-08 INET_NTOP(3)INET_PTON(3) Linux Programmer's Manual INET_PTON(3)NAMEinet_pton - convert IPv4 and IPv6 addresses from text to binary formSYNOPSIS#include <arpa/inet.h>int inet_pton(int af, const char *src, void *dst);DESCRIPTIONThis function converts the character string src into a network addressstructure in the af address family, then copies the network addressstructure to dst. The af argument must be either AF_INET or AF_INET6.The following address families are currently supported:AF_INETsrc points to a character string containing an IPv4 networkaddress in dotted-decimal format, "ddd.ddd.ddd.ddd", where dddis a decimal number of up to three digits in the range 0 to 255.The address is converted to a struct in_addr and copied to dst,which must be sizeof(struct in_addr) (4) bytes (32 bits) long.AF_INET6src points to a character string containing an IPv6 networkaddress. The address is converted to a struct in6_addr andcopied to dst, which must be sizeof(struct in6_addr) (16) bytes(128 bits) long. The allowed formats for IPv6 addresses followthese rules:1. The preferred format is x:x:x:x:x:x:x:x. This form consistsof eight hexadecimal numbers, each of which expresses a16-bit value (i.e., each x can be up to 4 hex digits).2. A series of contiguous zero values in the preferred formatcan be abbreviated to ::. Only one instance of :: can occurin an address. For example, the loopback address0:0:0:0:0:0:0:1 can be abbreviated as ::1. The wildcardaddress, consisting of all zeros, can be written as ::.3. An alternate format is useful for expressing IPv4-mapped IPv6addresses. This form is written as x:x:x:x:x:x:d.d.d.d,where the six leading xs are hexadecimal values that definethe six most-significant 16-bit pieces of the address (i.e.,96 bits), and the ds express a value in dotted-decimal nota‐tion that defines the least significant 32 bits of theaddress. An example of such an address is::FFFF:204.152.189.116.See RFC 2373 for further details on the representation of IPv6addresses.RETURN VALUEinet_pton() returns 1 on success (network address was successfully con‐verted). 0 is returned if src does not contain a character string rep‐resenting a valid network address in the specified address family. Ifaf does not contain a valid address family, -1 is returned and errno isset to EAFNOSUPPORT.ATTRIBUTESFor an explanation of the terms used in this section, seeattributes(7).┌────────────┬───────────────┬────────────────┐│Interface │ Attribute │ Value │├────────────┼───────────────┼────────────────┤│inet_pton() │ Thread safety │ MT-Safe locale │└────────────┴───────────────┴────────────────┘
CONFORMING TOPOSIX.1-2001, POSIX.1-2008.NOTESUnlike inet_aton(3) and inet_addr(3), inet_pton() supports IPv6addresses. On the other hand, inet_pton() accepts only IPv4 addressesin dotted-decimal notation, whereas inet_aton(3) and inet_addr(3) allowthe more general numbers-and-dots notation (hexadecimal and octal num‐ber formats, and formats that don't require all four bytes to beexplicitly written). For an interface that handles both IPv6addresses, and IPv4 addresses in numbers-and-dots notation, see getad‐drinfo(3).BUGSAF_INET6 does not recognize IPv4 addresses. An explicit IPv4-mappedIPv6 address must be supplied in src instead.EXAMPLEThe program below demonstrates the use of inet_pton() and inet_ntop(3).Here are some example runs:$ ./a.out i6 0:0:0:0:0:0:0:0::$ ./a.out i6 1:0:0:0:0:0:0:81::8$ ./a.out i6 0:0:0:0:0:FFFF:204.152.189.116::ffff:204.152.189.116Program source#include <arpa/inet.h>#include <stdio.h>#include <stdlib.h>#include <string.h>intmain(int argc, char *argv[]){unsigned char buf[sizeof(struct in6_addr)];int domain, s;char str[INET6_ADDRSTRLEN];if (argc != 3) {fprintf(stderr, "Usage: %s {i4|i6|<num>} string\n", argv[0]);exit(EXIT_FAILURE);}domain = (strcmp(argv[1], "i4") == 0) ? AF_INET :(strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);s = inet_pton(domain, argv[2], buf);if (s <= 0) {if (s == 0)fprintf(stderr, "Not in presentation format");elseperror("inet_pton");exit(EXIT_FAILURE);}if (inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL) {perror("inet_ntop");exit(EXIT_FAILURE);}printf("%s\n", str);exit(EXIT_SUCCESS);}SEE ALSOgetaddrinfo(3), inet(3), inet_ntop(3)COLOPHONThis page is part of release 4.04 of the Linux man-pages project. Adescription of the project, information about reporting bugs, and thelatest version of this page, can be found athttp://www.kernel.org/doc/man-pages/.Linux 2015-08-08 INET_PTON(3)
inet_ntop函数和inet_pton函数相关推荐
- inet_pton函数和inet_ntop函数的用法及简单实现
http://blog.csdn.net/eagle51/article/details/53157643?utm_source=itdadao&utm_medium=referral 这两个 ...
- getsockname函数与getpeername函数的使用
https://www.tuicool.com/articles/V3Avey getsockname和getpeername函数 getsockname函数用于获取与某个套接字关联的本地协议地址 ...
- render函数和redirect函数的区别+反向解析
render函数和redirect函数的区别+反向解析 1.视图函数:一定是要包含两个对象的(render源码里面有HttpResponse对象) request对象:----->所有的请求 ...
- Python day10 global关键字、函数递归、匿名函数、map函数的用法详解
1.global关键字 引用全局变量,在局部全局变量改变,也会改变,global相当于指针,将地址指向全局变量的name name='littlepage'def littepage():global ...
- C++ 笔记(13)— 函数(函数声明、函数定义、函数调用[传值、指针、引用]、函数参数默认值、函数重载)
每个 C++ 程序都至少有一个函数,即主函数 main() ,所有简单的程序都可以定义其他额外的函数. 1. 函数声明 函数声明告诉编译器函数的名称.返回类型和参数.函数声明包括以下几个部分: ret ...
- Go 学习笔记(16)— 函数(02)[函数签名、有名函数、匿名函数、调用匿名函数、匿名函数赋值给变量、匿名函数做回调函数]
1. 函数签名 函数类型也叫做函数签名,可以使用 fmt.Printf("%T") 格式化参数打印函数类型. package mainimport "fmt"f ...
- Go 学习笔记(15)— 函数(01)[函数定义、函数特点、多值返回、实参形参、变长参数,函数作为参数调用]
1. 函数定义 Go 语言最少有个 main() 函数.函数声明告诉了编译器函数的名称,返回类型和参数. func funcName(parameter_list)(result_list) {fun ...
- MySQL 学习笔记(3)— 字符串函数、数值函数、日期时间函数、流程函数、聚集函数以及分组数据
1. 字符串函数 MySQL 的常用函数包括字符串函数.数值函数.日期时间函数.流程函数等. SELECT ascii("abc"),char(97),concat("h ...
- 经常可能会用到的【函数节流和函数防抖】记录下,做下区分
今天突然被人问到,函数节流和函数防抖的区别是什么, 结果我脑子一热直接举了个滚动条的粟子说是优化高频率执行的手段,就记得自己是用setTimeout来实现的. 完了区别是什么??哪个是哪个都蒙B了 回 ...
最新文章
- 微服务平台的发展趋势
- 2020-11-13size_t和int
- (原創) 如何正確的使用迴圈(使用for_each)? (C/C++) (STL) (template)
- python def函数报错详解_python所有内置函数的定义详解
- 12.JDK1.8 JVM运行时数据区域概览、各区域介绍、程序计数器、Java虚拟机栈、本地方法栈、堆、堆空间内存分配(默认情况下)、字符串常量池、元数据区、jvm参数配置
- ios 获取最后一个cell_关于ios:向UICollectionView的第一个和最后一个单元格添加填充...
- ASP.NET Core Linux下为 dotnet 创建守护进程(必备知识)
- python判断密码强度_python实现密码强度校验
- 阿里云高效基因序列检索助力新冠肺炎病毒序列快速分析
- Spring 的下载、安装和使用
- python用户取消了安装_python的安装
- 数据类型之集合 set 运算关系
- Html5用户注册页面
- 10246 - Asterix and Obelix
- 测试--插拔寿命测试
- 用支付宝和微信都可以扫的聚合支付码其原理是什么?云收呗的原理也很简单,API搭建比较方便,云收呗是最大的黑天鹅
- spring boot 搭建测试报错Whitelabel Error Page No message available
- 基于STM32F103的直流电机调速系统
- 第2章-系统控制原理 -> 经典控制理论
- 基础(一)十六进制转八进制