Linux Network Programming [1] socket address

Source: Internet
Author: User

  • Three Linux socket addresses
  • Common socket address of sockaddr
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 */}
  • Socket address of sockaddr_in Internet
/* 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 the size of the struct sockaddr */}; struct in_addr {unit32_t s_addr; /* Internet address */};
  • Socket address of the sockaddr_un UNIX System
Struct sockaddr_un {sa_family_t sun_family;/* pf_unix or af_unix */Char sun_path [unix_path_max];/* path */};

  • Address Translation in Linux socket

In general, the X86 architecture PC uses the small-end mode to store data, while in the socket, the large-end mode is required, so the relevant conversion is required. Common conversion functions include:

  • Socket universal conversion functions

Ntohl converts 32-bit network data into host data

Ntohs converts 16-bit network data into host data

Htonl converts 32-bit host data into network data

Htons converts 16-bit network data into network data

  • Inet Conversion Function

Inet_ntoa converts the IP address of the network byte to a decimal vertex address.

Inet_aton converts the dotted-decimal address to the IP address of the network byte.

Inet_network converts the dot-decimal to 32-bit data of host bytes.

  • Summary

Generally, the sockaddr_in structure is used when network programming is used to fill in the value. When passed in as a function parameter, it is converted to the sockaddr structure, which is a string of 16 characters.Long.

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.