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, ¶m_init._address_dest.sin_addr.S_un.S_addr);
inet_pton(AF_INET, param._pip4_local, ¶m_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;