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{
char* h_name; //正式主机名
char** h_aliases; //别名,字符串数组
int h_addrtype; //协议类型
int h_length; //网络地址大小
char** h_addr_list;//指向网络地址的指针
};

功能

域名解析

备注

查看域名和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)_端口绑定和域名解析的更多相关文章

  1. 第14章 UDP编程(1)_UDP客户端服务器模型

    1. UDP编程模型 (1)UDP客户端服务器模型 ①客户端可以不调用bind()而直接与服务器通讯. ②UDP是无连接的,因此服务端不需要调用accept和listen,客户端也无需调用connec ...

  2. 第14章 UDP编程(3)_利用UDP实现广播功能

    3. 广播的介绍 (1)广播 ①广播实现一对多的通信,如QQ群 ②它通过向广播地址发送数据报文实现的 (2)SO_BROADCAST选项 ①SO_BROADCAST选项控制着UDP套接字是否能发送广播 ...

  3. 《Android开发艺术探索》读书笔记 (13) 第13章 综合技术、第14章 JNI和NDK编程、第15章 Android性能优化

    第13章 综合技术 13.1 使用CrashHandler来获取应用的Crash信息 (1)应用发生Crash在所难免,但是如何采集crash信息以供后续开发处理这类问题呢?利用Thread类的set ...

  4. 老李推荐:第14章1节《MonkeyRunner源码剖析》 HierarchyViewer实现原理-面向控件编程VS面向坐标编程

    老李推荐:第14章1节<MonkeyRunner源码剖析> HierarchyViewer实现原理-面向控件编程VS面向坐标编程   poptest是国内唯一一家培养测试开发工程师的培训机 ...

  5. 《mysql必知必会》学习_第14章_20180806_欢

    第14章:使用子查询. 子查询是镶嵌在其他查询里面,相当其他的select查询的条件来. P91 select order_num from where prod_id='tnt2';   #检索条件 ...

  6. Windows核心编程:第14章 探索虚拟内存

    Github https://github.com/gongluck/Windows-Core-Program.git //第14章 探索虚拟内存.cpp: 定义应用程序的入口点. // #inclu ...

  7. Linux就这个范儿 第14章 身在江湖

    Linux就这个范儿 第14章 身在江湖 “有人的地方就有江湖”,如今的计算机世界就像一个“江湖”.且不说冠希哥有多么无奈,把微博当QQ的局长有多么失败,就说如此平凡的你我什么时候就成了任人摆布的羔羊 ...

  8. TODO:Golang语言TCP/UDP协议重用地址端口

    TODO:Golang语言TCP/UDP协议重用地址端口 这是一个简单的包来解决重用地址的问题. go net包(据我所知)不允许设置套接字选项. 这在尝试进行TCP NAT时尤其成问题,其需要在同一 ...

  9. 《深入浅出Node.js》第7章 网络编程

    @by Ruth92(转载请注明出处) 第7章 网络编程 Node 只需要几行代码即可构建服务器,无需额外的容器. Node 提供了以下4个模块(适用于服务器端和客户端): net -> TCP ...

随机推荐

  1. 各种C#数组的定义和初始化

    各种C#数组的定义和初始化2009-08-26 18:28 岁月联盟 字号:T | T一键收藏,随时查看,分享好友!本文介绍了C#数组定义和初始化,包括一维数组.交错数组和多维数组,供大家参考.AD: ...

  2. 简单shell实现局域网IP扫描

    #!/bin/bash network=$1 time=$(date +%H%M%S) for i in $(seq $2 $3) do ping -c 1 -w 2 $network.$i > ...

  3. makefile for opencv

    makefile #################################################### # Generic makefile - 万能Makefile # for ...

  4. STM32 LSM6DSL 陀螺仪数据采集

    /************************************************************************************ * STM32 LSM6DS ...

  5. 【01_292】Nim Game

    Nim Game Total Accepted: 25342 Total Submissions: 50672 Difficulty: Easy You are playing the followi ...

  6. CTF-练习平台-Misc之 妹子的陌陌

    二十五.妹子的陌陌 该图片后缀名为rar,发现里面有一个文本 但是解压需要密码,应为不知道是几位的没法爆破,观察图片后发现红色字体:“喜欢我吗.”尝试一下,居然是密码,将文本解压出来 内容如下: 嘟嘟 ...

  7. cglib 多重 代理示例-2

    from:  http://thinkinjava.cn/2018/10/%E4%BD%BF%E7%94%A8-Cglib-%E5%AE%9E%E7%8E%B0%E5%A4%9A%E9%87%8D%E ...

  8. JQuery实时监控文本框字符变化

    $(function(){ $('input[name="addr"]').on('input propertychange', function() { if ($('input ...

  9. FastAdmin 浏览器 JS CSS 缓存如何更新?

    由于代码修改,但文件名没有修改,因为浏览器对 JS 和 CSS 是缓存的,而且由于服务器无法控制客户端的缓存. 但是可以对 JS 和 CSS 的请求加上版本号,达到更新缓存的效果.

  10. tomcat源码阅读之Server和Service接口解析

    tomcat中的服务器组件接口是Server接口,服务接口是Service,Server接口表示Catalina的整个servlet引擎,囊括了所有的组件,提供了一种优雅的方式来启动/关闭Catali ...