Common socket Data Structures

Source: Internet
Author: User

Struct sockaddr {

Unsigned short sa_family;/* address family, af_xxx */

Char sa_data [14];/* 14 bytes of Protocol address */

};

This data structure uses parameters for functions such as bind, connect, recvfrom, and sendto to specify the address information.

But in general programming, instead of directly operating on this data structure, it uses another addr_in data structure equivalent to sockaddr (defined in netinet/in. h ):

Struct sockaddr_in {

Short int sin_family;/* address family */

Unsigned short int sin_port;/* Port Number */

Struct in_addr sin_addr;/* Internet address */

Unsigned char sin_zero [8];/* same size as struct sockaddr */

};

In programming, most of them use the sockaddr_in structure to set/obtain address information.

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

Struct in_addr {

Unsigned long s_addr;

};

This data structure is retained for historical reasons and is mainly used for compatibility with previous formats.

S_addr stores IP addresses in byte sequence.

Sin_zero is an empty byte reserved to keep sockaddr and sockaddr_in data structures of the same size.

Example of setting address information:

struct sockaddr_in SA;

SA. sin_family = af_inet;
SA. sin_port = htons (3490);/* short, nbo */
SA. sin_addr.s_addr = inet_addr ("132.241.5.10");
bzero (& (SA. sin_zero), 8);

Note: If SA. sin_addr.s_addr = inaddr_any, no IP address is specified (for server Program )

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.