in_addr
sockaddr
sockaddr_in
结构体
typedef unsigned char __uint8_t;
typedef short __int16_t;
typedef unsigned short __uint16_t;
typedef int __int32_t;
typedef unsigned int __uint32_t;
typedef long long __int64_t;
typedef unsigned long long __uint64_t;
typedef __uint8_t sa_family_t;
typedef __uint16_t in_port_t;
typedef __uint32_t in_addr_t;
struct in_addr {
in_addr_t s_addr;
} ;
struct sockaddr {
__uint8_t sa_len;
sa_family_t sa_family;
char sa_data[ 14 ] ;
} ;
struct sockaddr_in {
__uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_zero[ 8 ] ;
} ;
typedef struct in6_addr {
union {
__uint8_t __u6_addr8[ 16 ] ;
__uint16_t __u6_addr16[ 8 ] ;
__uint32_t __u6_addr32[ 4 ] ;
} __u6_addr;
} in6_addr_t;
struct sockaddr_in6 {
__uint8_t sin6_len;
sa_family_t sin6_family;
in_port_t sin6_port;
__uint32_t sin6_flowinfo;
struct in6_addr sin6_addr;
__uint32_t sin6_scope_id;
} ;
typedef __uint8_t sa_family_t;
typedef __uint16_t in_port_t;
typedef __uint32_t in_addr_t;
struct in_addr {
unsigned int s_addr;
} ;
struct sockaddr {
unsigned char sa_len;
unsigned char sa_family;
char sa_data[ 14 ] ;
} ;
struct sockaddr_in {
unsigned char sin_len;
unsigned char sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_zero[ 8 ] ;
} ;
typedef struct in6_addr {
union {
unsigned char __u6_addr8[ 16 ] ;
unsigned short __u6_addr16[ 8 ] ;
unsigned int __u6_addr32[ 4 ] ;
} __u6_addr;
} in6_addr_t;
struct sockaddr_in6 {
unsigned char sin6_len;
unsigned char sin6_family;
in_port_t sin6_port;
unsigned int sin6_flowinfo;
struct in6_addr sin6_addr;
unsigned int sin6_scope_id;
} ;
inet_ntoa
inet_aton
inet_addr
inet_pton
inet_ntop
demo
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
int main ( int argc, const char * argv[ ] )
{
char szStrIp[ ] = "192.168.8.217" ;
unsigned int ulIp = 3232237785 ;
printf ( "ip: %s, dec: %u, little-endian-hex: %#x, big-endian-hex: %#x\n" , szStrIp, ulIp, ulIp, htonl ( ulIp) ) ;
char * pcTmp = NULL ;
int lRet = 0 ;
sockaddr stSockAddr = { 0 } ;
in_addr stInAddr = { 0 } ;
stInAddr. s_addr = inet_addr ( "192.168.8.217" ) ;
pcTmp = inet_ntoa ( stInAddr) ;
printf ( "inet_ntoa, ret: %s, s_addr: %#x.\n" , pcTmp, stInAddr. s_addr) ;
stInAddr. s_addr = inet_addr ( "259.255.255.255" ) ;
printf ( "inet_addr s_addr: %#x.\n" , stInAddr. s_addr) ;
char szIp[ INET_ADDRSTRLEN] = "192.168.8.217" ;
in_addr stOutAddr = { 0 } ;
lRet = inet_aton ( szIp, & stOutAddr) ;
printf ( "inet_aton ret: %d, ip: %s, s_addr: %#x.\n" , lRet, szIp, stOutAddr. s_addr) ;
in_addr stAddr2 = { 0 } ;
char szIp2[ INET_ADDRSTRLEN] = "192.168.8.217" ;
lRet = inet_pton ( AF_INET, szIp2, & stAddr2) ;
printf ( "inet_pton ret: %d, ip: %s, s_addr: %#x.\n" , lRet, szIp2, stAddr2. s_addr) ;
const char * pcTmp2 = NULL ;
memset ( szIp2, 0 , sizeof ( szIp2) ) ;
pcTmp2 = inet_ntop ( AF_INET, & stAddr2, szIp2, INET_ADDRSTRLEN) ;
printf ( "inet_ntop ret: %s, ip: %s.\n" , pcTmp2, szIp2) ;
return 0 ;
}
运行结果
ip: 192.168.8.217, dec: 3232237785, little-endian-hex: 0xc0a808d9, big-endian-hex: 0xd908a8c0
inet_ntoa, ret: 192.168.8.217, s_addr: 0xd908a8c0.
inet_addr s_addr: 0xffffffff.
inet_aton ret: 1, ip: 192.168.8.217, s_addr: 0xd908a8c0.
inet_pton ret: 1, ip: 192.168.8.217, s_addr: 0xd908a8c0.
inet_ntop ret: 192.168.8.217, ip: 192.168.8.217.
Program ended with exit code: 0