网络编程之新函数inet_pton和inet_ntop


  

1、头文件
 1 #include <arpe/inet.h> 

 

2、inet_pton 函数

 A、原型

 1 int inet_pton(int family, const char *strptr, void *addrptr);  

  B、功能 : 将点分十进制的ip地址转化为用于网络传输的数值格式

  C、返回值:   

    1)、成功则为1,

    2)、输入不是有效的表达式则为0,

    3)、出错则为-1

 

3、inet_ntop函数

  A、原型

 1 const char * inet_ntop(int family, const void *addrptr, char *strptr, size_t len);  

  B、功能: 将数值格式转化为点分十进制的ip地址格式

  C、返回值

    1)、成功则为指向结构的指针

    2)、出错则为NULL

 

4、inet_pton 用法(范例)

  A、旧版写法

    param_init._address_dest.sin_addr.S_un.S_addr         = inet_addr(param._pip4_dst);
    param_init._address_local.sin_addr.S_un.S_addr        = inet_addr(param._pip4_local);

  B、新版写法

    inet_pton(AF_INET, param._pip4_dst,           &param_init._address_dest.sin_addr.S_un.S_addr);
    inet_pton(AF_INET, param._pip4_local,        &param_init._address_local.sin_addr.S_un.S_addr);

  C、_pip4_dst 和 _pip4_local的定义

1         // the ip of destination
2         char _pip4_dst[ip4_len_16];
3 
4         // the ip of local
5         char _pip4_local[ip4_len_16];

  D 、_address_dest 和 _address_local 的定义

1         // the destination adress 
2         struct sockaddr_in        _address_dest;
3 
4         // local adress
5         struct sockaddr_in        _address_local;

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM