博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSAPP读书笔记11-01
阅读量:7021 次
发布时间:2019-06-28

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

CSAPP读书笔记11-01

最近找工作的时候发现自己以前看的网络编程方面的知识长时间没有回顾,忘记了不少。找来CSAPP直接看看网络部分相关内容,看的是英文版本,虽然有点慢,但收益颇多。

13.3 IP网络概览

13.3.1 IP地址

  • 使用32位IP地址映射所有主机
  • 使用域名标识IP地址
  • 因特网上所有主机之间可以使用IP地址进行通信

    struct in_addr {      unsignend int s_addr; /*大端模式(big endian)*/  }                               /*头文件netinet/in.h */

    现实中不同主机存在不同的字节序,大端模式和小端模式。为了统一相关的数据表达方式,

    定义网络字节序(大端模式)。

    #include 
    unsigned long int htonl(unsigned long int hostlong); unsigned short int htons(unsigned short int hostshort); Returns: value in network byte order unsigned long int ntohl(unsigned long int netlong); unsigned short int ntohs(unsigned short int netshort); Returns: value in host byte order

    htonl将32位整数主机字节序转化成网络字节序。htons将16位整数数转化为16位的网络字节序。相对的,ntohl将32位网络字节序转化为32位的主机字节序,nstoh则表示将16位的网络字节序转化为16位的主机字节序。(ps: h---host 本地主机;to---到;n---net 网络;l 是 unsigned long;s表示short;l 表示long)。为了便于人们建议,一般将IP地址用点画十进制来表示。例如,将0x8002c2f2表示成128.2.194.242。在linux下可以使用下面的命令查看本机的ip地址。

    wolf@wolf:~$ hostname -i  127.0.0.1

    如果我们需要将点画十进制和32位整数进行相互转化,可以使用下面的函数:

    #include 
    int inet_aton(const char *cp, struct in_addr *inp); Returns: 1 if OK, 0 on error char *inet_ntoa(struct in_addr in); Returns: pointer to a dotted-decimal string

13.3.2域名

无意义的整数系统对于人的记忆系统来说还是比较难以记忆的,所有为了便于人类记忆,于是设计了一套域名管理机制,将域名与IP地址进行映射。

/* DNS host entry structure */    struct hostent {        char    *h_name;    /* Official domain name of host */        char    **h_aliases;/* Null-terminated array of domain names */        int     h_addrtype; /* Host address type (AF_INET) */        int     h_length;   /* Length of an address, in bytes */        char    **h_addr_list; /* Null-terminated array of in_addr structs */};                                                      /*头文件 netdb.h*/

hostent数据结构是host entry简写。网络程序通过gethostbyname或者gethostbyaddr向DNS服务器检索任意的域名信息。

#include 
struct hostent *gethostbyname(const char *name); Returns: non-NULL pointer if OK, NULL pointer on error with h_errno set struct hostent *gethostbyaddr(const char *addr, int len, 0); Returns: non-NULL pointer if OK, NULL pointer on error with h_errno set

实例:

1/******************************************************************************     2 * FileName: gethostinfo.c     3 * Desc:      4 * Author: Zhang Wenwei     5 * Email: 1026367056@qq.com     6 * HomePage: http://www.cnblogs.com/ti1023/     7 * Version: 0.0.1     8 * LastChange: 2016-09-24 14:03:58     9 * History:     10 ******************************************************************************/     11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 18 int main(int argc, char **argv) 19 { 20 char **pp; 21 struct in_addr addr; 22 struct hostent *hostp; 23 24 if(argc != 2) { 25 printf("usage: %s
\n", argv[1]); 26 return 0; 27 } 28 29 if(inet_aton(argv[1], &addr) != 0) 30 hostp = gethostbyaddr((const char *)&addr, sizeof(addr), AF_INET); 31 else 32 hostp = gethostbyname(argv[1]); 33 printf("Offical hostname:%s\n", hostp->h_name); 34 for(pp = hostp->h_aliases; *pp != NULL; pp++) 35 printf("alias: %s\n", *pp); 36 37 for(pp = hostp->h_addr_list; *pp != NULL; pp++) { 38 addr.s_addr = ((struct in_addr *)*pp)->s_addr; 39 printf("%s\n", inet_ntoa(addr)); 40 } 41 return 0; 42 }

运行结果:

wolf@wolf:~/Workspace/gethost$ ./gethostinfo  www.google.comOffical hostname:www.google.com93.46.8.89wolf@wolf:~/Workspace/gethost$

转载于:https://www.cnblogs.com/ti1023/p/5903208.html

你可能感兴趣的文章
需要总结的知道
查看>>
「视频直播技术详解」系列之四:推流和传输
查看>>
微信小程序之快速接入七牛云
查看>>
cursor.MySQLCursorDict Class
查看>>
一行代码实现java list去重
查看>>
《zw版·Halcon-delphi系列原创教程》 Halcon分类函数008,matrix,矩阵函数
查看>>
[Codeforces Round #351 Div. 2] 673A Bear and Game
查看>>
php base64图片保存
查看>>
轻松掌握Ajax.net系列教程四:用Ajax.net实现客户端回调(Callback)
查看>>
Kinect+OpenNI学习笔记之3(获取kinect的数据并在Qt中显示的类的设计)
查看>>
整理 读过感觉不错的深度学习博客(更新中)
查看>>
响应在此上下文中不可用
查看>>
4、HTTP(下)
查看>>
redis持久化
查看>>
2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 1009
查看>>
poj3368(RMQ——ST)
查看>>
PHP解说日全食红月
查看>>
mybatis根据property获取column
查看>>
Windows Docker 安装
查看>>
CallableStatement调用Oracle存储过程返回结果集
查看>>