nc 用于和服务器建立连接。
在Ubuntu上面做实验
启动一个终端
再开启另一个终端
| 12
 3
 4
 5
 6
 7
 8
 
 | Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      -
 tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -
 tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
 tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -
 tcp        0      0 127.0.0.1:39351         0.0.0.0:*               LISTEN      -
 tcp        0      0 172.16.64.227:33574     111.3.88.228:80         TIME_WAIT   -
 tcp        0      0 172.16.64.227:48534     36.152.44.95:80         ESTABLISHED 3366/nc
 
 | 
可以看到最后一行,已经建立了一个TCP连接。
在刚才连接好百度服务器的终端上面,按http协议,请求百度首页。GET / HTTP/1.0并连续输入两个回车。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 
 | $ nc www.baidu.com 80GET / HTTP/1.0
 
 HTTP/1.0 200 OK
 Accept-Ranges: bytes
 Cache-Control: no-cache
 Content-Length: 9508
 Content-Type: text/html
 Date: Sun, 20 Mar 2022 05:48:08 GMT
 P3p: CP=" OTI DSP COR IVA OUR IND COM "
 P3p: CP=" OTI DSP COR IVA OUR IND COM "
 Pragma: no-cache
 Server: BWS/1.1
 Set-Cookie: BAIDUID=D9BC4FD3680ED7E670988E2B1D948396:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
 Set-Cookie: BIDUPSID=D9BC4FD3680ED7E670988E2B1D948396; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
 Set-Cookie: PSTM=1647755288; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
 Set-Cookie: BAIDUID=D9BC4FD3680ED7E6F640384383D296BE:FG=1; max-age=31536000; expires=Mon, 20-Mar-23 05:48:08 GMT; domain=.baidu.com; path=/; version=1; comment=bd
 Traceid: 1647755288023296769011190918765782309790
 Vary: Accept-Encoding
 X-Frame-Options: sameorigin
 X-Ua-Compatible: IE=Edge,chrome=1
 
 <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always" name="referrer"><meta name="description" content="全球领先的中文搜索引擎、致力于让网民更便捷地获取信息,找到所求。百度超过千亿的中文网页数据库,可以瞬间找到相关的搜索结果。">
 ...
 
 | 
写一个SocketDemo Server端程序,开启一个监听。
-ff 追踪线程和子线程
-o output
| 1
 | $ strace -ff -o ./out java SocketDemo 
 | 
通过jps或netstat可以看到一个pid
在fd目录下面,可以看到开启了多少个IO。
io流
/dev/pts/0 标准输入
/dev/pts/1 标准输出
/dev/pts/2 标准报错
open files 1024
在task目录,显示所有的线程
追踪一下redis的多路复用
| 1
 | $ strace -ff -o ./redis-out bin/redis-server config/server.properties
 | 
通过netstat查看进程监听状态
通过/proc/[pid]查看 fd 和 task,可以看到redis是多线程的。