The difference between sockaddr and sockaddr_in

Source: Internet
Author: User
Tags htons

Original address


struct sockaddr and struct sockaddr_in are two structures used to handle the addresses of network traffic.

In various system calls or functions, these two structures are used whenever dealing with network addresses.

The address in the network contains 3 aspects of the attribute:

1 Address Type: IPv4 or IPv6

2 IP Address

3 ports

Accordingly, the header file has the following definition:

1 include <netinet/in.h>
 2 
 3 struct sockaddr {
 4     unsigned short    sa_family;    2 bytes Address family, af_xxx
 5     char              sa_data[14];     bytes of protocol address
 6};
 7 
 8//IPV4 af_inet sockets:
 9 
struct sockaddr_in {One
short            sin_family;       2 bytes e.g Af_inet, af_inet6
unsigned short   sin_port;    2 bytes e.g htons (3490)-     struct in_addr   sin_addr;     4 bytes See struct in_addr, below     char             sin_zero[8];     8 bytes Zero If you want to
: 
in_addr struct {     unsigned long s_addr;          4 bytes load with Inet_pton ()
19};

The comments indicate the meaning of the attribute and its byte size, which are as large as 16 bytes and have the family attribute, but the difference is:

SOCKADDR uses the remaining 14 bytes to represent Sa_data, while sockaddr_in splits 14 bytes into Sin_port, sin_addr, and Sin_zero

Indicates the port, IP address, respectively. Sin_zero is used to fill bytes so that sockaddr_in and sockaddr remain the same size.

Sockaddr and sockaddr_in contain the same data, but they differ in their use:

Programmers should not operate SOCKADDR,SOCKADDR is for the operating system

Programmers should use sockaddr_in to represent addresses, sockaddr_in distinguish between addresses and ports, and are more convenient to use.

The general usage is:

The programmer populates the SOCKADDR_IN structure with the type, IP address, port, and then casts it to SOCKADDR, which is passed as a parameter to the system call function.

A typical code in network programming is:

1 int sockfd;
 2 struct sockaddr_in servaddr;
 3 
 4 sockfd = Socket (af_inet, sock_stream, 0);
 5 
 6/* Fill struct sockaddr_in *
 /7 bzero (&servaddr, sizeof (SERVADDR));
 8 servaddr.sin_family = af_inet;
 9 Servaddr.sin_port = htons (serv_port);
Ten Inet_pton (Af_inet, "127.0.0.1", &servaddr.sin_addr); 
12/* Cast to struct sockaddr *
/Connect (SOCKFD, (struct sockaddr *) &servaddr, sizeof (SERVADDR));
14  

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.