socket编程常用的数据结构

1:通用的socket地址格式:

 

struct sockaddr {
unsigned short sa_family;     /* address family, AF_xxx */
char sa_data[14];                 /* 14 bytes of protocol address, consist of IP and port*/
};

2:网络协议族格式的socket(sockaddr_in(在netinet/in.h中定义):)


struct sockaddr_in {
short int sin_family;                      /* Address family */
unsigned short int sin_port;       /* Port number */
struct in_addr sin_addr;              /* Internet address */
unsigned char sin_zero[8];         /* Same size as struct sockaddr */
};

 

其中sin_port结构体

typedef struct in_addr {
union {
            struct{
                        unsigned char s_b1,
                        s_b2,
                        s_b3,
                        s_b4;
                        } S_un_b;
           struct {
                        unsigned short s_w1,
                        s_w2;
                        } S_un_w;
            unsigned long S_addr;
          } S_un;
} IN_ADDR;

 

3:本地unix域格式的socket

 

  struct sockaddr_un {
  sa_family_t sun_family; /*PF_UNIX或AF_UNIX */
  char sun_path[UNIX_PATH_MAX]; /* 路径名 */
  };

 

程序中:

struct sockaddr_in serv_addr;

struct sockaddr_in remote_addr;

 

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(httpreg.puPort);

serv_addr.sin_addr.s_addr = inet_addr("192.168.2.142");

bzero(&(serv_addr.sin_zero), 8);

 

inet_addr()——将IP地址从 点数格式转换成无符号长整型

inet_ntoa()——作用和inet_addr()相反

 

 

 

你可能感兴趣的:(数据结构,编程,struct,socket,unix,网络协议)