网络编程之新函数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;

 

posted @ 2020-08-19 13:49  mohist  阅读(2732)  评论(0)    收藏  举报