Linux socket address structures: sockaddr and sockaddr_in, sockaddr_un

Source: Internet
Author: User
Tags unix domain socket

Struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14 bytes of Protocol address */
};
Sa_family is the address family, generally in the form of "af_xxx. It seems that af_inet is commonly used.
Sa_data is a 14-byte Protocol address.
This data structure uses parameters for functions such as bind, connect, recvfrom, and sendto to specify the address information.

However, in general programming, we do not directly operate on this data structure, but use another data structure equivalent to sockaddr.
Sockaddr_in (defined in netinet/in. h ):
Struct sockaddr_in {
Short int sin_family;/* address family */
Unsigned short int sin_port;/* Port Number */2 bytes
Struct in_addr sin_addr;/* Internet address */4 bytes
Unsigned char sin_zero [8];/* same size as struct sockaddr */8 bytes
};
Struct in_addr {
Unsigned long s_addr;
};

Typedef struct in_addr {
Union {
Struct {
Unsigned char s_b1,
S_b2,
S_b3,
S_b4;
} S_un_ B;
Struct {
Unsigned short s_w1,
S_w2;
} S_un_w;
Unsigned long s_addr;
} S_un;
} In_addr;

Sin_family refers to the protocol family. It can only be af_inet in socket programming.
Sin_port storage port number (in bytes)
Sin_addr storage IP address, using the in_addr Data Structure
Sin_zero is an empty byte reserved to keep sockaddr and sockaddr_in data structures of the same size.
S_addr stores IP addresses in byte sequence.

Sockaddr_in and sockaddr are parallel structures. The pointer to the addr_in struct can also point
Add struct and replace it. That is to say, you can use sockaddr_in to create the information you need,
Bzero (char *) & mysock, sizeof (mysock) can be used for type conversion at the end; // Initialization
Mysock struct name
Mysock. sa_family = af_inet;
Mysock. sin_addr.s_addr = inet_addr ("192.168.0.1 ");
......
To use the following code for conversion:
(Struct sockaddr *) mysock

Add an introduction to the addr_un struct:
Sockaddr_un

// Used for Unix domain socket local socket

UNIX socket address record.

Declaration

Source Position: socketsh. inc line 152

Type sockaddr_un =
Packed record

Sun_family: sa_family_t; // The type of sa_family_t is word, that is, unsiged short.

 

Address Family

Sun_path: Array
[0 .. 107]
Of char;

 

File Name

End;

Description

Sockaddr_unIs used to store a UNIX socket addres for the bind, Recv and send CILS.

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.