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. Centos编译Unix网络编程(第三版)卷1的源代码

    测试环境:Centos 1)在shell中输入./configure然后按回车(注意先让configure有执行权限 chomd 777 configure) 2)依次进入lib.libfree.li ...

  2. ubunttu-sh: 1: pause: not found

    old code: //in ubuntu OS system("pause"); error discription: : pause: not found right code ...

  3. win10系统如何进入BIOS模式

    前言 安装双系统时,一般需要设置一些BOOT的选项值,比如security boot的选项.以前都是重启之后按F2或者F12等进入BIOS模式的, 但是博主按照这种方式没有正确进入,本文就针对这一问题 ...

  4. xgboost 简单测试

    #coding=utf8 import pandas as pd from sklearn.model_selection import train_test_split from sklearn.f ...

  5. Spring boot 2.1.0 -- swagger2 整合

    依赖版本信息 Spring boot 2.1.0.RELEASE swagger2 2.7.0 1. mvn 配置  pom.xml 包引入 <!--swagger2依赖--> <d ...

  6. 在VM上安装centOS后的网络配置

    花了点时间,研究了下VM上的linux虚拟机的网络配置问题.1.环境主机:winXP SP2,家庭宽带,局域网连到路由器,ip地址为192.168.1.101. 虚拟机:centOS(redhat l ...

  7. ThinkPHP3.2.3整合smarty模板(三)

    在smarty模板中使用thinkphp框架的U方法时要主要的问题: 1.不能直接使用{:U('Index/index')}: 2.正确的使用方法为:<!--{U("Login/log ...

  8. MySQL删除超大表操作

    ======================================================================== 问题原因 通常情况下,会使用innodb_file_p ...

  9. 脸部识别JavaScript类库Tracking.js

    作者 王文刚 发布于 2014年8月10日 |   对Web开发者而言,开源的JavaScript库Tracking.js正在使计算机视觉和增强现实技术变得简单, 使用它可以展示效果类似Kinect或 ...

  10. mysql 行号 获取指定行数据

    mysql 行号的实现 Select id,(@rowNum:=@rowNum+1) as rowNo From first,(Select (@rowNum :=0) ) bOrder by fir ...