自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

窺探 Socket 監(jiān)聽(tīng)的秘密

安全 數(shù)據(jù)安全
我們來(lái)看unix網(wǎng)絡(luò)編程這本書(shū)是怎樣對(duì)它的解釋?zhuān)簂isten函數(shù)把一個(gè)未連接的套接字轉(zhuǎn)換成一個(gè)被動(dòng)套接字,指示內(nèi)核應(yīng)該接受指向該套接字的鏈接請(qǐng)求。

 [[416995]]

本文轉(zhuǎn)載自微信公眾號(hào)「盼盼編程」,作者盼盼編程。轉(zhuǎn)載本文請(qǐng)聯(lián)系盼盼編程公眾號(hào)。

socket用listen函數(shù)監(jiān)聽(tīng),listen從英語(yǔ)上理解就是一個(gè)"聽(tīng)"函數(shù),實(shí)際上它也就是這個(gè)意思。

我們來(lái)看unix網(wǎng)絡(luò)編程這本書(shū)是怎樣對(duì)它的解釋?zhuān)簂isten函數(shù)把一個(gè)未連接的套接字轉(zhuǎn)換成一個(gè)被動(dòng)套接字,指示內(nèi)核應(yīng)該接受指向該套接字的鏈接請(qǐng)求。

該函數(shù)有2個(gè)參數(shù),第一個(gè)我就不說(shuō)了,第二參數(shù)規(guī)定了內(nèi)核為相應(yīng)套接字排隊(duì)的最大連接個(gè)數(shù)。只看這些理論搞的人稀里糊涂,我們還是來(lái)測(cè)一下。

  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]$ 

請(qǐng)注意上面的服務(wù)端中,我是沒(méi)有調(diào)用accept函數(shù)的,直接調(diào)用getchar()了,跑起來(lái)。

  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. 服務(wù)度開(kāi)啟,然后新打開(kāi)一個(gè)窗口開(kāi)啟客戶(hù)端。 
  9. [mapan@localhost TCP]$ cd ../test/ 
  10. [mapan@localhost test]$  
  11. [mapan@localhost test]$ ./client 127.0.0.1 

查看網(wǎng)絡(luò):

  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]$ 

看,已經(jīng)建立起一個(gè)連接了。但是我們沒(méi)有調(diào)用accept函數(shù),連接還是建立起來(lái)了,這說(shuō)說(shuō)明accept函數(shù)和TCP三次握手沒(méi)啥關(guān)系,這也是一個(gè)知識(shí)盲點(diǎn)。好,在開(kāi)啟一個(gè)新窗口運(yùn)行客戶(hù)端,查看網(wǎng)絡(luò)狀態(tài)。(新開(kāi)窗口運(yùn)行客戶(hù)端同上,這里就不用代碼演示了)

  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 

看,又建立起一個(gè)連接。在運(yùn)行一個(gè)客戶(hù)端,看網(wǎng)絡(luò)狀態(tài)。

  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 

當(dāng)?shù)谌齻€(gè)客戶(hù)端連接進(jìn)來(lái)的時(shí)候,出現(xiàn)了一個(gè)SYN_RECV,這標(biāo)明第三個(gè)客戶(hù)端沒(méi)有與服務(wù)端建立連接。

我們listen函數(shù)設(shè)置的監(jiān)聽(tīng)隊(duì)列為1,那么監(jiān)聽(tīng)隊(duì)列塞了2個(gè)之后就沒(méi)有往里面塞了。這下大概懂了listen函數(shù)第二個(gè)參數(shù)的意義了吧,當(dāng)參數(shù)為1的時(shí)候只能監(jiān)聽(tīng)2個(gè)套接字,這應(yīng)該是從0開(kāi)始數(shù)的。

為什么是大概呢?其實(shí)unix網(wǎng)絡(luò)編程上是這樣說(shuō)的:listen函數(shù)的第二個(gè)參數(shù)是ESTABLISHED和SYN_RECV之和,只是在監(jiān)聽(tīng)隊(duì)列沒(méi)有滿(mǎn)的情況下,SYN_RECV狀態(tài)不容易重現(xiàn)。這時(shí)候在服務(wù)度輸入一個(gè)字符會(huì)有啥效果呢?

答案告訴你,就是那個(gè)SYN_RECV狀態(tài)變成ESTABLISHED了,這也是 accept函數(shù)的作用。accept函數(shù)會(huì)將已完成連接隊(duì)列中的對(duì)頭項(xiàng)返回給進(jìn)程,所以SYN_RECV變成ESTABLISHED了。這個(gè)現(xiàn)象留給大家去實(shí)踐一下吧,只有自己實(shí)踐出來(lái)的東西才是自己的。

 

責(zé)任編輯:武曉燕 來(lái)源: 盼盼編程
相關(guān)推薦

2020-09-07 19:40:06

監(jiān)聽(tīng)Facebook手機(jī)

2009-06-23 14:08:00

Java Socket

2013-01-08 09:37:26

大數(shù)據(jù)數(shù)據(jù)采集

2022-09-14 08:01:36

JoinMySQL迭代器

2011-06-09 10:20:43

朝鮮軟件開(kāi)發(fā)

2015-03-19 14:08:12

2013-10-30 09:42:38

Facebook圖搜索大數(shù)據(jù)

2016-12-08 16:47:06

2022-02-07 21:49:06

瀏覽器渲染chromium

2020-06-19 10:02:53

JVMJava語(yǔ)言

2011-11-15 08:53:52

用戶(hù)

2022-05-05 11:16:20

AI隱私算法

2017-05-16 09:56:44

2023-07-31 07:25:27

2014-12-17 10:04:19

2020-04-15 13:55:28

Kubernetes容器

2019-12-05 12:11:37

DevOps開(kāi)發(fā)應(yīng)用程序

2016-03-09 13:37:48

Twitter數(shù)據(jù)科學(xué)大數(shù)據(jù)

2013-11-27 11:04:05

震網(wǎng)病毒震網(wǎng)Stuxnet

2011-08-29 09:59:26

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)