Unix-sockets-socket-structures

提供:Dev Guides
移動先:案内検索

Unixソケット-構造

Unixソケットプログラミングでは、アドレスとポートに関する情報、およびその他の情報を保持するためにさまざまな構造が使用されます。 ほとんどのソケット関数では、引数としてソケットアドレス構造体へのポインターが必要です。 この章で定義されている構造は、インターネットプロトコルファミリに関連しています。

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 in

ソケットの要素を参照するのに役立つ2番目の構造は次のとおりです-

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.

addrに

この構造は、構造フィールドとして上記の構造でのみ使用され、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, finddevguides.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.

ソケット構造に関するヒント

ソケットアドレス構造は、すべてのネットワークプログラムの不可欠な部分です。 それらを割り当て、記入し、それらへのポインターをさまざまなソケット関数に渡します。 時々、これらの構造の1つへのポインターをソケット関数に渡して、内容を埋めます。

常に参照によってこれらの構造体を渡します(つまり、構造体自体ではなく構造体へのポインターを渡します)。また、常に構造体のサイズを別の引数として渡します。

ソケット関数が構造体を埋めると、その長さも参照によって渡されるため、その値は関数によって更新できます。 これらの値と結果の引数を呼び出します。

常に、bzero()関数にmemset()を使用して、構造変数をNULL(つまり、「\ 0」)に設定します。そうしないと、構造内に予期しないジャンク値を取得する可能性があります。