c语言 判断字符串是否是IP地址,判断输入的字符是不是为IP地址,并且判断该IP是否合法...

C/C++ codeNAME

inet_pton - convert IPv4 and IPv6 addresses from text to binary form

SYNOPSIS

#include

int inet_pton(int af, const char *src, void *dst);

DESCRIPTION

This function converts the character string src into a network address structure in the af address family, then copies the network address structure to

dst. The af argument must be either AF_INET or AF_INET6.

The following address families are currently supported:

AF_INET

src points to a character string containing an IPv4 network address in dotted-decimal format, "ddd.ddd.ddd.ddd", where ddd is 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_INET6

src points to a character string containing an IPv6 network address. The address is converted to a struct in6_addr and copied to dst, which must

be sizeof(struct in6_addr) (16) bytes (128 bits) long. The allowed formats for IPv6 addresses follow these rules:

1. The preferred format is x:x:x:x:x:x:x:x. This form consists of eight hexadecimal numbers, each of which expresses a 16-bit value (i.e., each

x can be up to 4 hex digits).

2. A series of contiguous zero values in the preferred format can be abbreviated to ::. Only one instance of :: can occur in an address. For

example, the loopback address 0:0:0:0:0:0:0:1 can be abbreviated as ::1. The wildcard address, consisting of all zeroes, can be written as

::.

3. An alternate format is useful for expressing IPv4-mapped IPv6 addresses. This form is written as x:x:x:x:x:x:d.d.d.d, where the six leading

xs are hexadecimal values that define the six most-significant 16-bit pieces of the address (i.e., 96 bits), and the ds express a value in

dotted-decimal notation that defines the least significant 32 bits of the address. An example of such an address is ::FFFF:204.152.189.116.

See RFC 2373 for further details on the representation of IPv6 addresses.

RETURN VALUE

inet_pton() returns 1 on success (network address was successfully converted). 0 is returned if src does not contain a character string representing a

valid network address in the specified address family. If af does not contain a valid address family, -1 is returned and errno is set to EAFNOSUPPORT.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值