窥探 Socket 监听的秘密

安全 数据安全
我们来看unix网络编程这本书是怎样对它的解释:listen函数把一个未连接的套接字转换成一个被动套接字,指示内核应该接受指向该套接字的链接请求。

 [[416995]]

本文转载自微信公众号「盼盼编程」,作者盼盼编程。转载本文请联系盼盼编程公众号。

socket用listen函数监听,listen从英语上理解就是一个"听"函数,实际上它也就是这个意思。

我们来看unix网络编程这本书是怎样对它的解释:listen函数把一个未连接的套接字转换成一个被动套接字,指示内核应该接受指向该套接字的链接请求。

该函数有2个参数,第一个我就不说了,第二参数规定了内核为相应套接字排队的最大连接个数。只看这些理论搞的人稀里糊涂,我们还是来测一下。

  1. [mapan@localhost test]$ ls 
  2. client.cpp  makefile  server.cpp 
  3. [mapan@localhost test]$  
  4. [mapan@localhost test]$ cat server.cpp  
  5. #include <unistd.h> 
  6. #include <sys/types.h> 
  7. #include <sys/socket.h> 
  8. #include <netdb.h> 
  9. #include <stdio.h> 
  10. #include <stdlib.h> 
  11. #include <string.h> 
  12. #include <ctype.h> 
  13. #include <errno.h> 
  14. #include <malloc.h> 
  15. #include <netinet/in.h> 
  16. #include <arpa/inet.h> 
  17. #include <sys/ioctl.h> 
  18. #include <stdarg.h> 
  19. #include <fcntl.h> 
  20. #include <sys/types.h> 
  21. #include <sys/wait.h> 
  22. #include <netinet/in.h> 
  23. #include <arpa/inet.h> 
  24. #include <signal.h> 
  25. #define MAXLINE 4096 
  26.  
  27.  
  28.  
  29. void main() 
  30.    int listenfd,connfd; 
  31.    socklen_t  clilen; 
  32.    struct sockaddr_in cliaddr,servaddr; 
  33.  
  34.    listenfd=socket(AF_INET,SOCK_STREAM,0); 
  35.    bzero(&servaddr,sizeof(servaddr)); 
  36.  
  37.    servaddr.sin_family=AF_INET; 
  38.    servaddr.sin_addr.s_addr=INADDR_ANY; 
  39.    servaddr.sin_port=htons(8888); 
  40.  
  41.    bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));   
  42.    listen(listenfd,1); 
  43.  
  44.    getchar(); 
  45.    connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen); 
  46.  
  47.  
  48.  
  49.    close(connfd); 
  50.    close(listenfd); 
  51. [mapan@localhost test]$ cat client.cpp  
  52. #include <unistd.h> 
  53. #include <sys/types.h> 
  54. #include <sys/socket.h> 
  55. #include <netdb.h> 
  56. #include <stdio.h> 
  57. #include <stdlib.h> 
  58. #include <string.h> 
  59. #include <ctype.h> 
  60. #include <errno.h> 
  61. #include <malloc.h> 
  62. #include <netinet/in.h> 
  63. #include <arpa/inet.h> 
  64. #include <sys/ioctl.h> 
  65. #include <stdarg.h> 
  66. #include <fcntl.h> 
  67. #include <sys/types.h> 
  68. #include <sys/wait.h> 
  69. #include <netinet/in.h> 
  70. #include <arpa/inet.h> 
  71. #include <signal.h> 
  72. #define MAXLINE 4096 
  73.  
  74.  
  75. void main() 
  76.    int sockfd; 
  77.    struct sockaddr_in servaddr; 
  78.  
  79.  
  80.    sockfd=socket(AF_INET,SOCK_STREAM,0); 
  81.    bzero(&servaddr,sizeof(servaddr)); 
  82.    servaddr.sin_family=AF_INET; 
  83.    servaddr.sin_port=htons(8888); 
  84.    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 
  85.  
  86.    int ret=connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); 
  87.    getchar(); 
  88.  
  89.    close(sockfd); 
  90. [mapan@localhost test]$ cat makefile  
  91. all:server client 
  92.  
  93. server.o:server.cpp 
  94.   g++ -c server.cpp 
  95. client.o:client.cpp 
  96.   g++ -c client.cpp 
  97. server:server.o 
  98.   g++ -o server server.o 
  99. client:client.o 
  100.   g++ -o client client.o 
  101.  
  102. clean: 
  103.   rm -f server client *.o 
  104. [mapan@localhost test]$ 

请注意上面的服务端中,我是没有调用accept函数的,直接调用getchar()了,跑起来。

  1. [mapan@localhost test]$ make 
  2. g++ -c server.cpp 
  3. g++ -o server server.o 
  4. g++ -c client.cpp 
  5. g++ -o client client.o 
  6. [mapan@localhost test]$ ./server  
  7.  
  8. 服务度开启,然后新打开一个窗口开启客户端。 
  9. [mapan@localhost TCP]$ cd ../test/ 
  10. [mapan@localhost test]$  
  11. [mapan@localhost test]$ ./client 127.0.0.1 

查看网络:

  1. [mapan@localhost test]$ netstat -na | grep 8888 
  2. tcp        0      0 0.0.0.0:8888                0.0.0.0:*                   LISTEN       
  3. tcp        0      0 127.0.0.1:34846             127.0.0.1:8888              ESTABLISHED  
  4. tcp        0      0 127.0.0.1:8888              127.0.0.1:34846             ESTABLISHED  
  5. [mapan@localhost test]$ 

看,已经建立起一个连接了。但是我们没有调用accept函数,连接还是建立起来了,这说说明accept函数和TCP三次握手没啥关系,这也是一个知识盲点。好,在开启一个新窗口运行客户端,查看网络状态。(新开窗口运行客户端同上,这里就不用代码演示了)

  1. [mapan@localhost test]$ netstat -na | grep 8888 
  2. tcp        0      0 0.0.0.0:8888                0.0.0.0:*                   LISTEN       
  3. tcp        0      0 127.0.0.1:34846             127.0.0.1:8888              ESTABLISHED  
  4. tcp        0      0 127.0.0.1:34848             127.0.0.1:8888              ESTABLISHED  
  5. tcp        0      0 127.0.0.1:8888              127.0.0.1:34846             ESTABLISHED  
  6. tcp        0      0 127.0.0.1:8888              127.0.0.1:34848             ESTABLISHED 

看,又建立起一个连接。在运行一个客户端,看网络状态。

  1. [mapan@localhost test]$ netstat -na | grep 8888 
  2. tcp        0      0 0.0.0.0:8888                0.0.0.0:*                   LISTEN       
  3. tcp        0      0 127.0.0.1:8888              127.0.0.1:34850             SYN_RECV     
  4. tcp        0      0 127.0.0.1:34846             127.0.0.1:8888              ESTABLISHED  
  5. tcp        0      0 127.0.0.1:34848             127.0.0.1:8888              ESTABLISHED  
  6. tcp        0      0 127.0.0.1:8888              127.0.0.1:34846             ESTABLISHED  
  7. tcp        0      0 127.0.0.1:8888              127.0.0.1:34848             ESTABLISHED  
  8. tcp        0      0 127.0.0.1:34850             127.0.0.1:8888              ESTABLISHED 

当第三个客户端连接进来的时候,出现了一个SYN_RECV,这标明第三个客户端没有与服务端建立连接。

我们listen函数设置的监听队列为1,那么监听队列塞了2个之后就没有往里面塞了。这下大概懂了listen函数第二个参数的意义了吧,当参数为1的时候只能监听2个套接字,这应该是从0开始数的。

为什么是大概呢?其实unix网络编程上是这样说的:listen函数的第二个参数是ESTABLISHED和SYN_RECV之和,只是在监听队列没有满的情况下,SYN_RECV状态不容易重现。这时候在服务度输入一个字符会有啥效果呢?

答案告诉你,就是那个SYN_RECV状态变成ESTABLISHED了,这也是 accept函数的作用。accept函数会将已完成连接队列中的对头项返回给进程,所以SYN_RECV变成ESTABLISHED了。这个现象留给大家去实践一下吧,只有自己实践出来的东西才是自己的。

 

责任编辑:武晓燕 来源: 盼盼编程
相关推荐

2020-09-07 19:40:06

监听Facebook手机

2009-06-23 14:08:00

Java Socket

2013-01-08 09:37:26

大数据数据采集

2022-09-14 08:01:36

JoinMySQL迭代器

2011-06-09 10:20:43

朝鲜软件开发

2015-03-19 14:08:12

2013-10-30 09:42:38

Facebook图搜索大数据

2016-12-08 16:47:06

2022-02-07 21:49:06

浏览器渲染chromium

2011-11-15 08:53:52

2022-05-05 11:16:20

AI隐私算法

2020-06-19 10:02:53

JVMJava语言

2017-05-16 09:56:44

2023-07-31 07:25:27

2020-04-15 13:55:28

Kubernetes容器

2019-12-05 12:11:37

DevOps开发应用程序

2014-12-17 10:04:19

2016-03-09 13:37:48

Twitter数据科学大数据

2009-05-31 15:21:24

一卡通安全

2010-11-04 10:19:31

Chrome OS
点赞
收藏

51CTO技术栈公众号