linux网络编程之sockaddr_in和in_addr区别
1、struct in_addr
struct in_addr就是32位IP地址。 struct in_addr { union { struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b; struct { u_short s_w1,s_w2; } S_un_w; u_long S_addr; } S_un; #define s_addr S_un.S_addr };
2、sockaddr_instruct sockaddr_in { short int sin_family; unsigned short int sin_port; struct in_addr sin_addr; unsigned char sin_zero[8]; };
sockaddr_in 结构体里面包含结构体in_addr,这是他们的关系
inet_addr()是将一个点分制的IP地址(如192.168.0.1)转换为上述结构中需要的32位IP地址(0xC0A80001)
一般用法struct sockaddr_in sockaddr; sockaddr.sin_family = AF_INET; sockaddr.sin_port = htons(MYPORT); sockaddr.sin_addr.s_addr = inet_addr("192.168.0.1");