Chinaunix首页 | 论坛 | 博客
  • 博客访问: 505686
  • 博文数量: 77
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 689
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-12 08:40
文章分类

全部博文(77)

文章存档

2018年(1)

2016年(3)

2015年(24)

2014年(49)

我的朋友

分类: 网络与安全

2015-02-03 15:05:23


int inet_aton(const char *cp, struct in_addr *inp);
in_addr_t inet_addr(const char *cp);
char *inet_ntoa(struct in_addr in);
host = gethostbyname(addr);

inet_aton() converts the Internet host address cp from  the  IPv4  num‐
       bers-and-dots  notation  into  binary  form (in network byte order) and
       stores it in the structure that inp  points  to.   inet_aton()  returns
       nonzero  if the address is valid, zero if not. 
       inet_aton(addr, &server_addr.sin_addr);
       memcpy(&server_addr.sin_addr, host->h_addr_list[0], host->h_length);


The inet_addr() function converts  the Internet host address cp from
       IPv4 numbers-and-dots notation into binary data in network byte  order.
       If  the input is invalid, INADDR_NONE (usually -1) is returned.  Use of
       this  function  is  problematic  because  -1   is   a   valid   address
       (255.255.255.255).    Avoid   its   use   in   favor   of  inet_aton(),
       inet_pton(3), or getaddrinfo(3) which provide a cleaner way to indicate
       error return. 
       server_addr.sin_addr.s_addr = inet_addr(addr);

The inet_ntoa() function converts the Internet host address  in,  given
       in  network  byte  order,  to a string in IPv4 dotted-decimal notation.
       The string is returned in a statically allocated buffer,  which  subse‐
       quent calls will overwrite.
       sprintf(addr,"%s",inet_ntoa(*((struct in_addr *)host->h_addr)));
       printf("addr:%s\n",addr);

Linux下这2个IP地址转换函数,可以在将IP地址在“点分十进制”和“整数”之间转换
而且,inet_pton和inet_ntop这2个函数能够处理ipv4和ipv6。算是比较新的函数了。

inet_pton函数原型如下[将"点分十进制" -> "整数"]

#include 
#include 
#include  int inet_pton(int af, const char *src, void *dst); //这个函数转换字符串到网络地址,第一个参数af是地址族,转换后存在dst中

inet_pton是inet_addr的扩展,支持的多地址族有下列:
af = AF_INET
src为指向字符型的地址,即ASCII的地址的首地址(ddd.ddd.ddd.ddd格式的),函数将该地址转换为in_addr的结构体,并复制在*dst中
af = AF_INET6
src为指向IPV6的地址,函数将该地址转换为in6_addr的结构体,并复制在*dst中
如果函数出错将返回一个负值,并将errno设置为EAFNOSUPPORT,如果参数af指定的地址族和src格式不对,函数将返回0。

inet_ntop函数原型如下[将"整数" -> "点分十进制"]

#include 
#include 
#include  const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt); //这个函数转换网络二进制结构到ASCII类型的地址,参数的作用和上面相同,只是多了一个参数socklen_t cnt, //他是所指向缓存区dst的大小,避免溢出,如果缓存区太小无法存储地址的值,则返回一个空指针,并将errno置为ENOSPC

下面是例程:

复制代码
#include 
#include 
#include <string.h>
#include 
#include 
#include in.h> 
int main (void)
{ 
    char IPdotdec[20]; // 存放点分十进制IP地址  
    struct in_addr s; // IPv4地址结构体 // 输入IP地址  
    printf("Please input IP address: ");
    scanf("%s", &IPdotdec); 
    // 转换  
    inet_pton(AF_INET, IPdotdec, (void *)&s);
    printf("inet_pton: 0x%x\n", s.s_addr); // 注意得到的字节序 
    // 反转换  
    inet_ntop(AF_INET, (void *)&s, IPdotdec, 16);
    printf("inet_ntop: %s\n", IPdotdec);
}
复制代码
阅读(2263) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~