c语言sscanf函数的用法是什么
291
2022-08-29
ipv4 ipv6 socket 编程相关结构
Structures for handling internet addresses
Prototypes
include
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 struct sockaddr_in and struct sockaddr_in6 share the same beginning structure as struct sockaddr
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*
So, with that in mind, remember that whenever a function says it takes a struct sockaddr* you can cast your struct sockaddr_in* , struct sockaddr_in6* , or struct sockadd_storage*
struct sockaddr_in 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 sin_zero field in struct sockaddr_in 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 struct in_addr 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.
struct sockadd_in6 and struct in6_addr
struct sockaddr_storage 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 struct sockaddr_storage structure is large enough to hold both types, unlike the original small struct sockaddr
Example
// IPv4:struct sockaddr_in ip4addr;int s;ip4addr.sin_family = AF_INET;ip4addr.sin_port = htons(3490);inet_pton(AF_INET, "10.0.0.1", &ip4addr.sin_addr);s = socket(PF_INET, SOCK_STREAM, 0);bind(s, (struct sockaddr*)&ip4addr, sizeof ip4addr);// IPv6:struct sockaddr_in6 ip6addr;int s;ip6addr.sin6_family = AF_INET6;ip6addr.sin6_port = htons(4950);inet_pton(AF_INET6, "2001:db8:8714:3a90::12", &ip6addr.sin6_addr);s = socket(PF_INET6, SOCK_STREAM, 0);bind(s, (struct sockaddr*)&ip6addr, sizeof ip6addr); struct sockaddr_in, struct in_addrStructures for handling internet addressesPrototypes #include
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*
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
-->
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)); |
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~