博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络编程中常见结构体
阅读量:6305 次
发布时间:2019-06-22

本文共 3006 字,大约阅读时间需要 10 分钟。

  hot3.png

include 
// All pointers to socket address structures are often cast to pointers// to this type before use in various functions and system calls:struct sockaddr { unsigned short sa_family; // address family, AF_xxx char sa_data[14]; // 14 bytes of protocol address};// IPv4 AF_INET sockets:struct sockaddr_in { short sin_family; // e.g. AF_INET, AF_INET6 unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // zero this if you want to};struct in_addr { unsigned long s_addr; // load with inet_pton()};// IPv6 AF_INET6 sockets:struct sockaddr_in6 { u_int16_t sin6_family; // address family, AF_INET6 u_int16_t sin6_port; // port number, Network Byte Order u_int32_t sin6_flowinfo; // IPv6 flow information struct in6_addr sin6_addr; // IPv6 address u_int32_t sin6_scope_id; // Scope ID};struct in6_addr { unsigned char s6_addr[16]; // load with inet_pton()};// General socket address holding structure, big enough to hold either// struct sockaddr_in or struct sockaddr_in6 data:struct sockaddr_storage { sa_family_t ss_family; // address family // all this is padding, implementation specific, ignore it: char __ss_pad1[_SS_PAD1SIZE]; int64_t __ss_align; char __ss_pad2[_SS_PAD2SIZE];};
// 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);
// IPv4 demo of inet_ntop() and inet_pton()struct sockaddr_in sa;char str[INET_ADDRSTRLEN];// store this IP address in sa:inet_pton(AF_INET, "192.0.2.33", &(sa.sin_addr));// now get it back and print itinet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);printf("%s\n", str); // prints "192.0.2.33"// IPv6 demo of inet_ntop() and inet_pton()// (basically the same except with a bunch of 6s thrown around)struct sockaddr_in6 sa;char str[INET6_ADDRSTRLEN];// store this IP address in sa:inet_pton(AF_INET6, "2001:db8:8714:3a90::12", &(sa.sin6_addr));// now get it back and print itinet_ntop(AF_INET6, &(sa.sin6_addr), str, INET6_ADDRSTRLEN);printf("%s\n", str); // prints "2001:db8:8714:3a90::12"
struct sockaddr_in antelope;char *some_addr;inet_aton("10.0.0.1", &antelope.sin_addr); // store IP in antelopesome_addr = inet_ntoa(antelope.sin_addr); // return the IPprintf("%s\n", some_addr); // prints "10.0.0.1"// and this call is the same as the inet_aton() call, above:antelope.sin_addr.s_addr = inet_addr("10.0.0.1");

 

转载于:https://my.oschina.net/tsh/blog/1486917

你可能感兴趣的文章
oracle表分区详解
查看>>
网络编程中常见结构体
查看>>
SSL/TLS原理详解
查看>>
Docker 自定义SSH服务镜像
查看>>
JavaScript强化教程 —— Cocos2d-JS自动JSB绑定规则修改
查看>>
configure: error: in `/root/httpd-2.2.11/srclib/apr': c
查看>>
CentOS7搭建Kubernetes-dashboard管理服务
查看>>
buildroot下查找外部编译器通过ext-toolchain-wrapper调用的参数
查看>>
MySQL Replication 主主配置详细说明
查看>>
Linux的任务调度
查看>>
在Android studio中添加jar包方法如下
查看>>
iframe 在ie下面总是弹出新窗口解决方法
查看>>
分享10款漂亮实用的CSS3按钮
查看>>
安装nginx 常见错误及 解决方法
查看>>
Gorun8电子商城
查看>>
在之前链表的基础上改良的链表
查看>>
android编译系统makefile(Android.mk)写法
查看>>
MD5源代码C++
查看>>
Eclipse 添加 Ibator
查看>>
Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
查看>>