struct sockaddr and struct sockaddr_in

Source: Internet
Author: User

In the Linux environment, struct 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, struct struct sockaddr_in is defined in/usr/include/netinet/in.h, as follows:
/* Structure describing an Internet socket address. */
struct SOCKADDR_IN
{

Short sin_family; /*addressfamily Generally speaking, af_inet (address family) pf_inet (protocol family) */

unsigned short sin_port; /* PortNumber (must use the network data format, the ordinary number can be converted to the network data format number using the Htons () function) */

struct IN_ADDR sin_addr; /* Internet address. */

unsigned char sin_zero[8];

/* The character array sin_zero[8] exists to ensure that the size of struct sockaddr_in is equal to the size of struct struct sockaddr */
};

The following is the data type used in the struct sockaddr_in, which is defined as follows:

struct IN_ADDR is actually a 32-bit IP address
struct IN_ADDR {
unsigned long s_addr;
};

struct SOCKADDR is a universal socket address, while struct sockaddr_in is the address form of a socket in the Internet environment, both of which are 16 bytes in length. The two are parallel structures, and pointers to sockaddr_in structures can also point to sockaddr. In general, it is necessary to cast the SOCKADDR_IN structure into the SOCKADDR structure and re-pass it into the system call function.

The BSD network software contains two functions for converting between binary address formats and dotted decimal string formats, but these two functions only support IPV4.
in_addr_t inet_addr (const char *CP);
Char *inet_ntoa (struct in_addr in);
Two functions with similar functions support both IPV4 and IPV6
const char *inet_ntop (int domain, const void *addr, char *str, socklen_t size);
int Inet_pton (int domain, const char *str, void *addr);

The usual usage is:
int sockfd;
struct sockaddr_in my_addr;
SOCKFD = socket (af_inet, sock_stream, 0);

my_addr.sin_family = af_inet; /* Host byte order */
My_addr.sin_port = htons (MyPort); /* short, network byte order */

MY_ADDR.SIN_ADDR.S_ADDR = inet_addr ("192.168.0.1");

Bzero (& (My_addr.sin_zero), 8); /* Zero the rest of the struct */
memset (&my_addr.sin_zero, 0, 8);

Bind (SOCKFD, (struct sockaddr *) &my_addr, sizeof (struct sockaddr));

Reference:

Relationship between struct sockaddr and struct sockaddr_in, struct Sockaddr_un-Leo Chin-Blog Park
Http://www.cnblogs.com/hnrainll/archive/2011/04/24/2026432.html

struct sockaddr and struct sockaddr_in

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.