第14章 UDP编程(2)_端口绑定和域名解析
2. 端口绑定和域名解析
2.1 端口绑定:SO_REUSEADDR选项
int opt = ;//1表示启用该选项
//设置为可重新使用端口,每次启动该端口时,会重新绑定端口。相当于端口被复位并被重新。
//绑定。因此,以后以最后这次绑定为准
if((ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt))) < ){
perror("setsockopt error");
exit();
}
2.2 域名解析
(1)域名解析过程

(2)域名解析函数
|
头文件 |
#include <netdb.h> |
|
函数 |
struct hostent* gethostent(void); struct hostent* gethostbyname(const char* hostname); void sethostent(int stayopen); void endhostent(void); //注意get和endhostent应成对使用! |
|
参数 |
struct hostent{
|
|
功能 |
域名解析 |
|
备注 |
查看域名和IP映射关系:#more /etc/hosts |
(3)hostent结构体

(4)域名解析案例
//域名解析,并将结果保存在hostent结构体中
if((hptr = gethostbyname("www.google.com")) == NULL){
fprintf(stderr, "gethostbyname call failed.%s\n",
hstrerror(h_errno));
return -;
} printf("official name: %s\n", hptr->h_name); //输出正式名
for(pptr = hptr->h_aliases; *pptr != NULL; pptr++){ //输出对应各个别名
printf("\t alias: %s\n", *pptr);
} if(hptr->h_addrtype != AF_INET){ //判断是否为IPv4地址
fprintf(stderr, "Invalid address type %d\n", hptr->addrtype);
return -;
} pptr = hptr->h_addr_list;
for(; *pptr != NULL; pptr++){ //输出点分十进制的IP地址(字符串)
printf("\t address: %s\n",
inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
}
【编程实验】根据域名解析出IP地址
//gethost.c
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h> int is_host(struct hostent* host, char* name)
{
//如果正式名为name,则直接返回
if(!strcmp(host->h_name, name)) return ; //查找别名为name
int i = ;
while(host->h_aliases[i] != NULL){
if(!strcmp(host->h_aliases[i], name)) return ;
i++;
}
} //根据主机名获取IP
unsigned int get_ip_by_name(char* name)
{
unsigned int ip = ;
struct hostent* host;
//遍历/etc/hosts文件,找出所有的主机名。
//每调用一次gethostent将读取文件的一行(对应一个主机)
while((host = gethostent()) != NULL){
if(is_host(host, name)){
//h_addr_list[0]中所存放在IP是网络字节序,共4个字节。
memcpy(&ip, host->h_addr_list[], );
break;
}
}
endhostent();
return ip;
} void out_addr(struct hostent* h)
{
printf("hostname: %s\n", h->h_name);
printf("addrtype: %s\n", h->h_addrtype == AF_INET ? "IPv4": "IPv6"); char ip[];
memset(ip, , sizeof(ip));
inet_ntop(h->h_addrtype, h->h_addr_list[], ip, sizeof(ip));//绑定的第1个IP地址
printf("ip address: %s\n", ip); //别名
int i = ;
while(h->h_aliases[i] != NULL){
printf("aliases: %s\n", h->h_aliases[i]);
i++;
}
} int main(int argc, char* argv[])
{
if(argc < ){
printf("usage: %s host\n", argv[]);
exit();
} struct hostent* h;
h = gethostbyname(argv[]); if(h != NULL){
out_addr(h);
}else{
printf("no %s exist\n", argv[]);
} //根据主机名,输出IP
unsigned int ipNet = get_ip_by_name(argv[]); //网络字节序IP
char ip[];
memset(ip, , sizeof(ip));
inet_ntop(AF_INET, &ipNet, ip, sizeof(ip));//绑定的第1个IP地址
printf("ip address: %s\n", ip); return ;
}
/*host文件
* [root@localhost 14.udp]# more /etc/hosts
* 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
* 192.168.32.100 santaclaus www.5iedu.com
* ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
*
*输出结果
[root@localhost 14.udp]# bin/gethost santaclaus
hostname: santaclaus
addrtype: IPv4
ip address: 192.168.32.100
aliases: www.5iedu.com
ip address: 192.168.32.100
[root@localhost 14.udp]# bin/gethost www.5iedu.com
hostname: santaclaus
addrtype: IPv4
ip address: 192.168.32.100
aliases: www.5iedu.com
ip address: 192.168.32.100
[root@localhost 14.udp]# bin/gethost localhost
hostname: localhost
addrtype: IPv4
ip address: 127.0.0.1
aliases: localhost.localdomain
aliases: localhost4
aliases: localhost4.localdomain4
aliases: localhost.localdomain
aliases: localhost6
aliases: localhost6.localdomain6
ip address: 127.0.0.1
*/
第14章 UDP编程(2)_端口绑定和域名解析的更多相关文章
- 第14章 UDP编程(1)_UDP客户端服务器模型
1. UDP编程模型 (1)UDP客户端服务器模型 ①客户端可以不调用bind()而直接与服务器通讯. ②UDP是无连接的,因此服务端不需要调用accept和listen,客户端也无需调用connec ...
- 第14章 UDP编程(3)_利用UDP实现广播功能
3. 广播的介绍 (1)广播 ①广播实现一对多的通信,如QQ群 ②它通过向广播地址发送数据报文实现的 (2)SO_BROADCAST选项 ①SO_BROADCAST选项控制着UDP套接字是否能发送广播 ...
- 《Android开发艺术探索》读书笔记 (13) 第13章 综合技术、第14章 JNI和NDK编程、第15章 Android性能优化
第13章 综合技术 13.1 使用CrashHandler来获取应用的Crash信息 (1)应用发生Crash在所难免,但是如何采集crash信息以供后续开发处理这类问题呢?利用Thread类的set ...
- 老李推荐:第14章1节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-面向控件编程VS面向坐标编程
老李推荐:第14章1节<MonkeyRunner源码剖析> HierarchyViewer实现原理-面向控件编程VS面向坐标编程 poptest是国内唯一一家培养测试开发工程师的培训机 ...
- 《mysql必知必会》学习_第14章_20180806_欢
第14章:使用子查询. 子查询是镶嵌在其他查询里面,相当其他的select查询的条件来. P91 select order_num from where prod_id='tnt2'; #检索条件 ...
- Windows核心编程:第14章 探索虚拟内存
Github https://github.com/gongluck/Windows-Core-Program.git //第14章 探索虚拟内存.cpp: 定义应用程序的入口点. // #inclu ...
- Linux就这个范儿 第14章 身在江湖
Linux就这个范儿 第14章 身在江湖 “有人的地方就有江湖”,如今的计算机世界就像一个“江湖”.且不说冠希哥有多么无奈,把微博当QQ的局长有多么失败,就说如此平凡的你我什么时候就成了任人摆布的羔羊 ...
- TODO:Golang语言TCP/UDP协议重用地址端口
TODO:Golang语言TCP/UDP协议重用地址端口 这是一个简单的包来解决重用地址的问题. go net包(据我所知)不允许设置套接字选项. 这在尝试进行TCP NAT时尤其成问题,其需要在同一 ...
- 《深入浅出Node.js》第7章 网络编程
@by Ruth92(转载请注明出处) 第7章 网络编程 Node 只需要几行代码即可构建服务器,无需额外的容器. Node 提供了以下4个模块(适用于服务器端和客户端): net -> TCP ...
随机推荐
- Ubuntu 16.04 安装 PyCharm
https://blog.csdn.net/zhuanshu666/article/details/73554885
- django model 不设置default='' 会出错
当django中的model没有设置default = ''时,会弹出提示信息.在model中加上默认值为空,这样就不会对数据库的必填字段留空造成错误,导致数据库出错,系统崩溃.
- QModelIndex 与 QStandardItem互转
1. QModelIndex 转换成QStandardItem QStandardItem * item=QStandardItemModel::itemFromIndex(const QModel ...
- 50个必备常用的jQuery代码段
1. 如何修改jQuery默认编码(例如默认UTF-8改成改GB2312): $.ajaxSetup({ ajaxSettings:{ contentType:"application/x- ...
- https页面证书验证、加密过程简介
1.服务器向CA机构获取证书(假设这个证书伪造不了),当浏览器首次请求服务器的时候,服务器返回证书给浏览器.(证书包含:公钥+申请者与颁发者的相关信息+签名) 2.浏览器得到证书后,开始验证证书的相关 ...
- Linux运维常用150个命令
Linux运维常用150个命令 转载自:www.cnblogs.com/bananaaa/p/7774467.html 命令 功能说明 线上查询及帮助命令(2个) man 查看命令帮助,命令的词典,更 ...
- OK335xS Ubuntu 12.04.1 版本 Android 开发环境搭建
/******************************************************************************************** * OK33 ...
- HDU 3068 最长回文 manacher 算法,基本上是O(n)复杂度
下面有别人的比较详细的解题报告: http://wenku.baidu.com/view/3031d2d3360cba1aa811da42.html 下面贴我的代码,注释在代码中: #include ...
- [转载]PHP 连接 Rabbitmq 实例代码
转自 http://www.dahouduan.com/2017/11/23/php-rabbitmq-demo/ 接下来我们用 php 连接 rabbitmq 玩一玩.还没有安装 rabbitmq ...
- gem install没有反应 解决办法
在虚拟机上执行gem install redis 没有反应: 百度方法如下: #移除官方镜像,增加淘宝镜像 gem sources --add https://ruby.taobao.org/ --r ...