inet_pton 和 inet_ntop 等函数
来源:互联网 发布:爱名网域名过户 编辑:程序博客网 时间:2024/06/04 00:21
inet_pton 和inet_ntop对IPv4和IPv6都适用
<arpa/inet.h>
头文件中的声明如下:
int inet_pton (int __af, const char *__restrict __cp, void *__restrict __buf)const char *inet_ntop (int __af, const void *__restrict __cp, char *__restrict __buf, size_t socklen_t__len)
man
SYNOPSIS #include <arpa/inet.h> 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.// inet_ntop NAME inet_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);DESCRIPTION This function converts the network address structure src in the af address family into a character string. The resulting string is copied to the buffer pointed to by dst, which must be a non-null pointer. The caller specifies the number of bytes available in this buffer in the argument size./*size 为目标储存单元的大小,即点分十进制的长度或冒号16进制的长度,为了有助于指定这个大小,在`<netinet/in.h>`中有如下定义:*/#define INET_ADDRSTRLEN 16#define INET6_ADDRSTRLEN 46
与书中有些出入,不过不影响。
字节操纵函数
为什么要这些函数?
因为有些字段(IP地址)可能包含值为0的字节,但却不是C字符串。不能用str开头的函数处理。
这些函数分为两类:
以b开头的第一组函数起源于4.2BSD:
#include <strings.h> //bzero - write zero-valued bytes void bzero(void *s, size_t n); //bcopy - copy byte sequence void bcopy(const void *src, void *dest, size_t n); // bcmp - compare byte sequences int bcmp(const void *s1, const void *s2, size_t n);//const限定被修饰的指针所指向的内容不能被函数改变
man
The bzero() function sets the first n bytes of area starting at s to zero (bytes containing '\0').The bcopy() function copies n bytes from src to dest. The result is correct, even when both areas overlap.DESCRIPTION The bcmp() function compares the two byte sequences s1 and s2 of length n each. If they are equal, and in particular if n is zero, bcmp() returns 0. Otherwise, it returns a nonzero result.RETURN VALUE The bcmp() function returns 0 if the byte sequences are equal, other-wise a nonzero result is returned.
以mem(memory)开头的第二组函数起源于ANSI标准:
#include <string.h>//memset - fill memory with a constant bytevoid *memset(void *s, int c, size_t n);//memcpy - copy memory areavoid *memcpy(void *dest, const void *src, size_t n);//memcmp - compare memory areasint memcmp(const void *s1, const void *s2, size_t n);
man
//memsetThe memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.//memcpyThe memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap.//memcmpDESCRIPTION The memcmp() function compares the first n bytes (each interpreted as unsigned char) of the memory areas s1 and s2.RETURN VALUE The memcmp() function returns an integer less than, equal to, or greater than zero if the first n bytes of s1 is found, respectively, to be less than, to match, or be greater than the first n bytes of s2. For a nonzero return value, the sign is determined by the sign of the difference between the first pair of bytes (interpreted as unsigned char) that differ in s1 and s2. If n is zero, the return value is zero.
未完
0 0
- inet_pton 和 inet_ntop 等函数
- inet_pton和inet_ntop函数
- inet_pton和inet_ntop函数
- inet_pton和inet_ntop函数
- inet_pton和inet_ntop函数
- inet_pton函数 和inet_ntop函数
- inet_pton函数 和inet_ntop函数
- inet_pton()和inet_ntop()函数详解
- inet_pton()和inet_ntop()函数详解
- Linux函数--inet_pton / inet_ntop
- inet_pton、inet_ntop函数
- inet_pton、inet_ntop函数
- Linux函数--inet_pton / inet_ntop
- inet_pton和inet_ntop
- inet_pton和inet_ntop
- inet_ntop,inet_pton(替代inet_aton等)
- 套接字API(九)inet_pton函数和inet_ntop函数
- inet_pton函数和inet_ntop函数的用法及简单实现
- hdu3341:Lost's revenge (AC自动机+DP)
- JAVA设计模式-适配器模式
- Shell脚本调试技术
- 用python scrapy 写东西用的小思路 Item Pipeline
- 历届试题 兰顿蚂蚁
- inet_pton 和 inet_ntop 等函数
- 三种基于感知哈希算法的相似图像检索技术
- linux服务器免密码登录实现
- HPUOJ1056题
- [勇者闯LeetCode] 121. Best Time to Buy and Sell Stock
- webstorm配置scss自动编译路径
- Ubuntu 16.10 禁用 Guest 访客模式
- Eclipse java web项目 ,导入IntelliJ IDEA 完整操作!
- SASS开发环境搭建(webstorm做编辑器)