The difference between sockaddr and sockaddr_in (reproduced)

Source: Internet
Author: User



Original link: http://kenby.iteye.com/blog/1149001



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:




include <netinet/in.h>

struct sockaddr {
    unsigned short    sa_family;    // 2 bytes address family, AF_xxx
    char              sa_data[14];     // 14 bytes of protocol address
};

// IPv4 AF_INET sockets:

struct sockaddr_in {
    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 this if you want to
};

struct in_addr {
    unsigned long s_addr;          // 4 bytes load with inet_pton()
};





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:

Related functions: Socket, accept, connect, listen

Header files: #include <sys/types.h> #include <sys/socket.h>

Define functions: int bind (int sockfd, struct sockaddr * my_addr, int addrlen);

Function Description: Bind () is used to set the name of the socket to the parameter sockfd. This name is pointed to by the parameter my_addr to a SOCKADDR structure and defines a common data structure for different socket domain



The difference between sockaddr and sockaddr_in (reproduced)


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.