11---
2- title : Linux高性能服务器编程读书记录
2+ title : Linux高性能服务器编程记录
33
44---
55开头选了这本书, 看到这本书是他人总结三大本后自己写的, 先求个理解大概.
@@ -11,9 +11,13 @@ title: Linux高性能服务器编程读书记录
1111# demo 记录
1212
13132019年8月20日 编写demo <1. 客户端自动发送固定信息> -- 所用基础连接部分和TCP连接部分
14+
14152019年8月22日 编写demo <2. 伪全双工TCP> -- 所用截止到网络Api之前
16+
15172019年8月22日 编写demo <3. 伪全双工UDP> -- 所用截止到网络Api之前
1618
19+ 2019年8月25日 复现 源码 <4. 代码清单5-12 访问daytime服务> -- 第五章结束
20+
1721## 客户端自动发送固定信息
1822
1923## 伪全双工TCP
@@ -302,10 +306,73 @@ struct linger {
302306```
303307#### 网络信息API
304308``` c
309+ #include < netdb.h>
310+ // 通过主机名查找ip
311+ struct hostent* gethostbyname (const char* name);
312+
313+ // 通过ip获取主机完整信息
314+ // type为IP地址类型 AF_INET和AF_INET6
315+ struct hostent* gethostbyaddr(const void* addr, size_t len, int type);
316+
317+ struct hostent {
318+ char* h_name;
319+ char ** h_aliases;
320+ int h_addrtype;
321+ int h_length;
322+ char** h_addr_list; /* 按网络字节序列出主机IP地址列表* /
323+ }
324+ ```
325+ 以下两个函数通过读取/etc/services文件 来获取服务信息
326+ ```c
327+ #include <netdb.h>
328+ // 根据名称获取某个服务的完整信息
329+ struct servent getservbyname(const char* name, const char* proto);
330+
331+ // 根据端口号获取服务信息
332+ struct servent getservbyport(int port, const char* proto);
333+
334+ struct servent {
335+ char* s_name; /* 服务名称*/
336+ char ** s_aliases; /* 服务的别名列表*/
337+ int s_port; /* 端口号*/
338+ char* s_proto; /* 服务类型, 通常为TCP或UDP*/
339+ }
340+ ```
341+ ``` c
342+ #include < netdb.h>
343+ // 内部使用的gethostbyname 和 getserverbyname
344+ // hostname 用于接收主机名, 也可以用来接收字符串表示的IP地址(点分十进制, 十六进制字符串)
345+ // service 用于接收服务名, 字符串表示的十进制端口号
346+ // hints参数 对getaddrinfo的输出进行更准确的控制, 可以设置为NULL, 允许反馈各种有用的结果
347+ // result 指向一个链表, 用于存储getaddrinfo的反馈结果
348+ int getaddrinfo (const char* hostname, const char* service, const struct addrinfo* hints, struct addrinfo** result)
349+
350+ struct addrinfo{
351+ int ai_flags;
352+ int ai_family;
353+ int ai_socktype; /* 服务类型, SOCK_STREAM或者SOCK_DGRAM* /
354+ int ai_protocol;
355+ socklen_t ai_addrlen;
356+ char* ai_canonname; /* 主机的别名* /
357+ struct sockaddr* ai_addr; /* 指向socket地址* /
358+ struct addrinfo* ai_next; /* 指向下一个结构体* /
359+ }
305360
361+ // 需要手动的释放堆内存
362+ void freeaddrinfo(struct addrinfo* res);
306363```
364+ 
307365
308- ![ ] ( Linux高性能服务器编程读书记录/socket选项.jpg )
366+ ```c
367+ #include <netdb.h>
368+ // host 存储返回的主机名
369+ // serv存储返回的服务名
370+
371+ int getnameinfo(const struct sockaddr* sockaddr, socklen_t addrlen, char* host, socklen_t hostlen, char* serv
372+ socklen_t servlen, int flags);
373+
374+ ```
375+ ![ ] ( https://ftp.bmp.ovh/imgs/2019/08/bc7196e9a30d5152.png )
309376
310377测试
311378使用
0 commit comments