日期:2014-05-16 浏览次数:20769 次
struct sockaddr { unsigned short sa_family; /* 地址族, AF_xxx */ char sa_data[14]; /* 14字节的协议地址*/ };
struct sockaddr_in { short int sin_family; /* 地址族 */ unsigned short int sin_port; /* 端口号 */ struct in_addr sin_addr; /* Internet地址 */ unsigned char sin_zero[8]; /* 与struct sockaddr一样的长度 */ };
struct in_addr { unsigned long s_addr; };
#include <arpa/inet.h> uint32_t htonl (uint32_t hostlong) //主机字节序到网络字节序的长整数转换 uint16_t htons (uint16_t hostshort) //主机字节序到网络字节序的短整型转换 uint32_t ntohl (uint32_t netlong) //网络字节序到主机字节序的长整形转换 uint16_t ntohs(uint16_t netshort) //网络字节序到主机字节序的短整型转换
#include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> int inet_aton (const char *str, struct in_addr *addr) //将字符串转换为in_addr值 in_addr inet_addr (const char *str) //将字符串转换为in_addr值 char *inet_ntoa (struct in_addr addr) //将in_addr的结构地址转换为字符串
int inet_pton(int af, const char *src, void *dst)
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
int sockfd; struct sockaddr_in my_addr; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } bzero(&my_addr, sizeof(my_addr)); my_addr.sin_family = AF_INET; my_addr.sin_port = htons(port); my_addr.sin_addr.s_addr = inet_addr("192.168.1.3");