The difference between struct sockaddr and struct sockaddr_in, struct sockaddr_un

Source: Internet
Author: User
Tags socket port number

In the Linux environment, the structure 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]; * Bytes of protocol address * *

};

In the Linux environment, the structure struct sockaddr_in is defined in/usr/include/netinet/in.h, as follows:

/* Structure describing an Internet sockets 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 sockaddr '. */

unsigned char sin_zero[sizeof (struct sockaddr)-

__sockaddr_common_size-

sizeof (in_port_t)-

sizeof (struct in_addr)];

/* character Array sin_zero[8] exists to ensure that the size of the structure struct sockaddr_in is equal to the size of the struct sockaddr of the structure body.

};

struct sockaddr is a generic socket address, and struct sockaddr_in is the address form of the socket in the Internet environment, which is 16 bytes in length. The two are parallel structures, and pointers to sockaddr_in structures can point to sockaddr. In general, you need to cast the SOCKADDR_IN structure into the SOCKADDR structure and then into the system call function.

The following two data types are used in the 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;

};

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.