Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

sockets - Understanding struct sockaddr

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

In this structure what exactly is the meaning address family depicted by sa_family?

Does it mean that protocols like TCP/UDP have "addresses"? Well, the protocols can be identification numbers not addresses, I think.

Anyway, if yes, then on what basis have their families been divided?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The format and size of the address is usually protocol specific.

sockaddr is used as the base of a set of address structures that act like a discriminated union, see the Beej guide to networking. You generally look at the sa_family and then cast to the appropriate address family's specific address structure.

TCP and UDP do not have addresses specific to them as such, rather the IP level has different sizes of address for IPv4 and IPv6.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...