简体   繁体   中英

sockaddr structure - (sys/socket.h)

Ive been reading the sys/socket.h all day and finally starting to understand it, and now starting to use it, however, I'm not sure why I can't assign a value to sa_family member of the sockaddr struct.

Specification sockaddr Structure:

struct sockaddr{
    sa_family_t   sa_family       address family
    char          sa_data[]       socket address (variable-length data)
};

Data Type: sa_family_t - Unsigned integral type (2-4 bytes)

Values:

Name                Purpose                          Man page
       AF_UNIX, AF_LOCAL   Local communication              unix(7)
       AF_INET             IPv4 Internet protocols          ip(7)
       AF_INET6            IPv6 Internet protocols          ipv6(7)
       AF_IPX              IPX - Novell protocols
       AF_NETLINK          Kernel user interface device     netlink(7)
       AF_X25              ITU-T X.25 / ISO-8208 protocol   x25(7)
       AF_AX25             Amateur radio AX.25 protocol
       AF_ATMPVC           Access to raw ATM PVCs
       AF_APPLETALK        AppleTalk                        ddp(7)
       AF_PACKET           Low level packet interface       packet(7)
       AF_ALG              Interface to kernel crypto API

Which is a bit confusing since these values are char data types not an unsigned int .

Simple Test:

#include<stdio.h>
#include<sys/socket.h> 

int main(void){
    struct sockaddr_in address;
    address.sin_family = AF_INET;
    printf("Socket Address Family: %s\n", address.sin_family);
    return 0;
}

Error:

storage size of 'address' isn't known

It should be printing out Socket Address Family: AF_INET ... what am I missing here?

It is because you don't include the definition of that structure. The structure sockaddr_in is defined in <netinet/in.h> . For more details, see: sockaddr_in undeclared identifier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM