Socket address Structure

Source: Internet
Author: User

Most socket functions require a pointer to the socket address structure as a parameter. Each protocol cluster defines its own socket address structure

The names of these structures begin with sockaddr_ and end with a unique suffix corresponding to each protocol cluster

1.ipv4 Socket Address Structure

IPV4 socket address structure is commonly referred to as: Internetwork Socket address Structure

It is named after the sockaddr_in

struct in_addr{

in_addr_t s_addr;//32 bit unsigned integer type

};

struct sockaddr_in{

unit8_t sin_len;//socket Address structure length it is an unsigned short shape that can not set its value

sa_family_t the address family of the sin_family;//socket address structure any unsigned integer type

in_port_t sin_port;//tcp or UDP port number network byte order at least 16-bit unsigned integer type

struct IN_ADDR sin_addr;//ipv4 address network byte order

Cahr sin_zero[8];//not have to be aligned

};

2. Universal socket Address Structure

When passed into any socket function as a parameter, the socket address structure is always passed as a reference (that is, a pointer to the struct).

However, any socket function with such a pointer as one of the parameters must handle the socket address structure from any of the supported protocol families

With ANSI C The workaround is simple: void * is a generic pointer type

However the socket function is defined before ANSI C to take the method of defining a universal socket address structure

struct sockaddr{

uint8_t Sa_len;

sa_family_t sa_family;//Address Cluster type

Cahr sa_data[14];//

}

The socket function is then defined as one of its parameters as a pointer to a common socket address structure

Socket address Structure

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.