📜  Unix套接字-结构

📅  最后修改于: 2020-11-05 04:51:18             🧑  作者: Mango


Unix套接字编程中使用了各种结构来保存有关地址和端口的信息以及其他信息。大多数套接字函数都需要一个指向套接字地址结构的指针作为参数。本章定义的结构与Internet协议族有关。

sockaddr

第一个结构是sockaddr ,它保存套接字信息-

struct sockaddr {
   unsigned short   sa_family;
   char             sa_data[14];
};

这是一个通用的套接字地址结构,它将在大多数套接字函数调用中传递。下表提供了成员字段的描述-

Attribute Values Description
sa_family

AF_INET

AF_UNIX

AF_NS

AF_IMPLINK

It represents an address family. In most of the Internet-based applications, we use AF_INET.
sa_data Protocol-specific Address The content of the 14 bytes of protocol specific address are interpreted according to the type of address. For the Internet family, we will use port number IP address, which is represented by sockaddr_in structure defined below.

sockaddr在

帮助您引用套接字元素的第二种结构如下-

struct sockaddr_in {
   short int            sin_family;
   unsigned short int   sin_port;
   struct in_addr       sin_addr;
   unsigned char        sin_zero[8];
};

这是成员字段的描述-

Attribute Values Description
sa_family

AF_INET

AF_UNIX

AF_NS

AF_IMPLINK

It represents an address family. In most of the Internet-based applications, we use AF_INET.
sin_port Service Port A 16-bit port number in Network Byte Order.
sin_addr IP Address A 32-bit IP address in Network Byte Order.
sin_zero Not Used You just set this value to NULL as this is not being used.

在地址

该结构仅在以上结构中用作结构字段,并拥有32位netid / hostid。

struct in_addr {
   unsigned long s_addr;
};

这是成员字段的描述-

Attribute Values Description
s_addr service port A 32-bit IP address in Network Byte Order.

主人

此结构用于保留与主机有关的信息。

struct hostent {
   char *h_name; 
   char **h_aliases; 
   int h_addrtype;  
   int h_length;    
   char **h_addr_list
    
#define h_addr  h_addr_list[0]
};

这是成员字段的描述-

Attribute Values Description
h_name ti.com etc. It is the official name of the host. For example, tutorialspoint.com, google.com, etc.
h_aliases TI It holds a list of host name aliases.
h_addrtype AF_INET It contains the address family and in case of Internet based application, it will always be AF_INET.
h_length 4 It holds the length of the IP address, which is 4 for Internet Address.
h_addr_list in_addr For Internet addresses, the array of pointers h_addr_list[0], h_addr_list[1], and so on, are points to structure in_addr.

– h_addr定义为h_addr_list [0],以保持向后兼容性。

仆人

此特定结构用于保留与服务和相关端口有关的信息。

struct servent {
   char  *s_name; 
   char  **s_aliases; 
   int   s_port;  
   char  *s_proto;
};

这是成员字段的描述-

Attribute Values Description
s_name http This is the official name of the service. For example, SMTP, FTP POP3, etc.
s_aliases ALIAS It holds the list of service aliases. Most of the time this will be set to NULL.
s_port 80 It will have associated port number. For example, for HTTP, this will be 80.
s_proto

TCP

UDP

It is set to the protocol used. Internet services are provided using either TCP or UDP.

插座结构技巧

套接字地址结构是每个网络程序的组成部分。我们分配它们,填写它们,并将指向它们的指针传递给各种套接字函数。有时,我们将指向这些结构之一的指针传递给套接字函数,并填充其中的内容。

我们总是通过引用传递这些结构(即,我们传递指向结构的指针,而不是结构本身),并且我们总是传递结构的大小作为另一个参数。

当套接字函数填充结构时,该长度也通过引用传递,以便可以通过该函数更新其值。我们称这些值为结果的参数。

始终通过对bzero()函数使用memset()将结构变量设置为NULL(即’\ 0’),否则它可能会在您的结构中获得意外的垃圾值。