ipv4 ipv6 socket 编程相关结构

Structures for handling internet addresses

Prototypes


Description

These are the basic structures for all syscalls and functions that deal with internet addresses. Often you'll usegetaddinfo() to fill these structures out, and then will read them when you have to.

In memory, the <nobr><tt class="type">struct sockaddr_in</tt></nobr> and <nobr><tt class="type">struct sockaddr_in6</tt></nobr> share the same beginning structure as<nobr><tt class="type">struct sockaddr</tt></nobr>, and you can freely cast the pointer of one type to the other without any harm, except the possible end of the universe.

Just kidding on that end-of-the-universe thing...if the universe does end when you cast a<nobr><tt class="type">struct sockaddr_in*</tt></nobr> to a<nobr><tt class="type">struct sockaddr*</tt></nobr>, I promise you it's pure coincidence and you shouldn't even worry about it.

So, with that in mind, remember that whenever a function says it takes a <nobr><tt class="type">struct sockaddr*</tt></nobr> you can cast your <nobr><tt class="type">struct sockaddr_in*</tt></nobr>,<nobr><tt class="type">struct sockaddr_in6*</tt></nobr>, or<nobr><tt class="type">struct sockadd_storage*</tt></nobr> to that type with ease and safety.

<nobr><tt class="type">struct sockaddr_in</tt></nobr> is the structure used with IPv4 addresses (e.g. "192.0.2.10"). It holds an address family (AF_INET), a port insin_port, and an IPv4 address insin_addr.

There's also this <nobr><tt class="type">sin_zero</tt></nobr> field in <nobr><tt class="type">struct sockaddr_in</tt></nobr> which some people claim must be set to zero. Other people don't claim anything about it (the Linux documentation doesn't even mention it at all), and setting it to zero doesn't seem to be actually necessary. So, if you feel like it, set it to zero usingmemset().

Now, that <nobr><tt class="type">struct in_addr</tt></nobr> is a weird beast on different systems. Sometimes it's a crazyunion with all kinds of#defines and other nonsense. But what you should do is only use thes_addr field in this structure, because many systems only implement that one.

<nobr><tt class="type">struct sockadd_in6</tt></nobr> and <nobr><tt class="type">struct in6_addr</tt></nobr> are very similar, except they're used for IPv6.

<nobr><tt class="type">struct sockaddr_storage</tt></nobr> is a struct you can pass toaccept() orrecvfrom() when you're trying to write IP version-agnostic code and you don't know if the new address is going to be IPv4 or IPv6. The<nobr><tt class="type">struct sockaddr_storage</tt></nobr> structure is large enough to hold both types, unlike the original small<nobr><tt class="type">struct sockaddr</tt></nobr>.

Example


Description 
 

These are the basic structures for all syscalls and functions that deal with internet addresses. In memory, the struct sockaddr_in is the same size as struct sockaddr, and you can freely cast the pointer of one type to the other without any harm, except the possible end of the universe.

Just kidding on that end-of-the-universe thing...if the universe does end when you cast a struct sockaddr_in* to a struct sockaddr*, I promise you it's pure coincidence and you shouldn't even worry about it.

So, with that in mind, remember that whenever a function says it takes a struct sockaddr* you can cast your struct sockaddr_in* to that type with ease and safety.

There's also this sin_zero field which some people claim must be set to zero. Other people don't claim anything about it (the Linux documentation doesn't even mention it at all), and setting it to zero doesn't seem to be actually necessary. So, if you feel like it, set it to zero using memset().

Now, that struct in_addr is a weird beast on different systems. Sometimes it's a crazy union with all kinds of #defines and other nonsense. But what you should do is only use the s_addr field in this structure, because many systems only implement that one.

With IPv4 (what basically everyone in 2005 still uses), the struct s_addr is a 4-byte number that represents one digit in an IP address per byte. (You won't ever see an IP address with a number in it greater than 255.)

-->
Example

struct sockaddr_in myaddr;
int s;

myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(3490);
inet_aton("63.161.169.137", &myaddr.sin_addr.s_addr);

s = socket(PF_INET, SOCK_STREAM, 0);
bind(s, (struct sockaddr*)myaddr, sizeof(myaddr));

你可能感兴趣的:(socket)