Difference and connection between struct sockaddr and struct sockaddr_in and struct sockaddr_un

Source: Internet
Author: User

In Linux, the struct sockaddr is defined in/usr/include/Linux/socket. H, as follows:
Typedef unsigned short sa_family_t;
Struct sockaddr {
Sa_family_t sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14 bytes of Protocol address */

In Linux, the struct sockaddr_in is defined in/usr/include/netinet/in. H, as follows:
/* Structure describing an internet socket address .*/
Struct sockaddr_in
{
_ Sockaddr_common (SIN _);
In_port_t sin_port;/* port number .*/
Struct in_addr sin_addr;/* Internet address .*/

/* Pad to size of 'struct sockadd '.*/
Unsigned char sin_zero [sizeof (struct sockaddr )-
_ Sockaddr_common_size-
Sizeof (in_port_t )-
Sizeof (struct in_addr)];
/* The character array sin_zero [8] exists to ensure that the size of the struct sockaddr_in is equal to that of the struct sockaddr */
};
Struct sockaddr is a common socket address, while struct sockaddr_in is a socket address in the Internet environment. The length of the socket is the same as 16 bytes. The two are parallel structures. The pointer to the sockaddr_in structure can also point to sockaddr. Generally, the sockaddr_in structure needs to be forcibly converted into a sockaddr structure and then passed into the system call function.

The following are two data types used in struct sockaddr_in, which are defined as follows:
/* Type to represent a port .*/
Typedef uint16_t in_port_t;

Struct in_addr is actually a 32-bit IP address.
Struct in_addr {
Unsigned long s_addr;
};

The BSD network software contains two functions for mutual conversion between the binary address format and the dot-decimal string format. However, these two functions only support IPv4.
In_addr_t inet_addr (const char * CP );
Char * inet_ntoa (struct in_addr in );
Two Functions with similar functions support both IPv4 and IPv6
Const char * inet_ntop (INT domain, const void * ADDR, char * STR, socklen_t size );
Int inet_ton (INT domain, const char * STR, void * ADDR );

The common usage is:
Int sockfd;
Struct sockaddr_in my_addr;
Sockfd = socket (af_inet, sock_stream, 0 );

My_addr.sin_family = af_inet;/* Host byte order */
My_addr.sin_port = htons (myport);/* short, network byte order */

My_addr.sin_addr.s_addr = inet_addr ("192.168.0.1 ");

Bzero (& (my_addr.sin_zero), 8);/* zero the rest of the struct */
// Memset (& my_addr.sin_zero, 0, 8 );

BIND (sockfd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr ));

# Define unix_path_max 108

Struct sockaddr_un {

Sa_family_t sun_family;/* pf_unix or af_unix */

Char sun_path [unix_path_max];/* path */

};

The struct sockaddr structure type is used to save socket information:
Struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */-- address format
Char sa_data [14];/* 14-byte Protocol address */-- address value (IP address and port number)
};

Sockfd is the socket descriptor returned by the socket function. my_addr is a sockaddr pointer pointing to information including the local IP address and port number. addrlen is often set to sizeof (struct sockaddr ).
The struct sockaddr structure type is used to save socket information:
Struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14-byte Protocol address */
};
Sa_family is generally af_inet, which represents the Internet (TCP/IP) address family. sa_data contains the IP address and port number of the socket.
There is also a structure type:
Struct sockaddr_in {
Short int sin_family;/* address family */
Unsigned short int sin_port;/* Port Number */
Struct in_addr sin_addr;/* IP Address */
Unsigned char sin_zero [8];/* fill in 0 to keep the same size as struct sockaddr */
};
This structure is more convenient to use. Sin_zero is used to fill the sockaddr_in structure with the same length as struct sockaddr. It can be set to zero using the bzero () or memset () function. The pointer to sockaddr_in and the pointer to sockaddr can be converted to each other, which means that if the parameter type required by a function is sockaddr, you can convert a pointer to sockaddr_in to a pointer to sockaddr when calling a function; or vice versa.

You just need to remember to use the sockaddr_in structure when filling in the value, and use it as the function's
The parameter can be converted to the sockaddr structure when it is passed in. After all, it is 16 characters long.
Long.

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 };

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.