listen函数

来源:互联网 发布:js实现数组去重 编辑:程序博客网 时间:2024/06/04 00:38

          listen函数,从英语上理解就是一个"听"函数,实际上它也就是这个意思。我们来看unix网络编程这本书是怎样对它的解释:listen函数把一个未连接的套接字转换成一个被动套接字,指示内核应该接受指向该套接字的链接请求。该函数有2个参数,第一个我就不说了,第二参数规定了内核为相应套接字排队的最大连接个数。只看这些理论搞的人稀里糊涂,我们还是来测一下。

[mapan@localhost test]$ lsclient.cpp  makefile  server.cpp[mapan@localhost test]$ [mapan@localhost test]$ cat server.cpp #include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <errno.h>#include <malloc.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/ioctl.h>#include <stdarg.h>#include <fcntl.h>#include <sys/types.h>#include <sys/wait.h>#include <netinet/in.h>#include <arpa/inet.h>#include <signal.h>#define MAXLINE 4096void main(){   int listenfd,connfd;   socklen_t  clilen;   struct sockaddr_in cliaddr,servaddr;   listenfd=socket(AF_INET,SOCK_STREAM,0);   bzero(&servaddr,sizeof(servaddr));   servaddr.sin_family=AF_INET;   servaddr.sin_addr.s_addr=INADDR_ANY;   servaddr.sin_port=htons(8888);   bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));     listen(listenfd,1);   getchar();   connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);   close(connfd);   close(listenfd);}[mapan@localhost test]$ cat client.cpp #include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <ctype.h>#include <errno.h>#include <malloc.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/ioctl.h>#include <stdarg.h>#include <fcntl.h>#include <sys/types.h>#include <sys/wait.h>#include <netinet/in.h>#include <arpa/inet.h>#include <signal.h>#define MAXLINE 4096void main(){   int sockfd;   struct sockaddr_in servaddr;   sockfd=socket(AF_INET,SOCK_STREAM,0);   bzero(&servaddr,sizeof(servaddr));   servaddr.sin_family=AF_INET;   servaddr.sin_port=htons(8888);   servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");   int ret=connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));   getchar();   close(sockfd);}[mapan@localhost test]$ cat makefile all:server clientserver.o:server.cppg++ -c server.cppclient.o:client.cppg++ -c client.cppserver:server.og++ -o server server.oclient:client.og++ -o client client.oclean:rm -f server client *.o[mapan@localhost test]$ 


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

[mapan@localhost test]$ makeg++ -c server.cppg++ -o server server.og++ -c client.cppg++ -o client client.o[mapan@localhost test]$ ./server 

服务度开启,然后新打开一个窗口开启客户端。

[mapan@localhost TCP]$ cd ../test/[mapan@localhost test]$ [mapan@localhost test]$ ./client 127.0.0.1

查看网络:

[mapan@localhost test]$ netstat -na | grep 8888tcp        0      0 0.0.0.0:8888                0.0.0.0:*                   LISTEN      tcp        0      0 127.0.0.1:34846             127.0.0.1:8888              ESTABLISHED tcp        0      0 127.0.0.1:8888              127.0.0.1:34846             ESTABLISHED [mapan@localhost test]$ 
看,已经建立起一个连接了。但是我们没有调用accept函数,连接还是建立起来了,这说说明accept函数和TCP三次握手没啥关系,这也是一个知识盲点。好,在开启一个新窗口运行客户端,查看网络状态。(新开窗口运行客户端同上,这里就不用代码演示了)

[mapan@localhost test]$ netstat -na | grep 8888tcp        0      0 0.0.0.0:8888                0.0.0.0:*                   LISTEN      tcp        0      0 127.0.0.1:34846             127.0.0.1:8888              ESTABLISHED tcp        0      0 127.0.0.1:34848             127.0.0.1:8888              ESTABLISHED tcp        0      0 127.0.0.1:8888              127.0.0.1:34846             ESTABLISHED tcp        0      0 127.0.0.1:8888              127.0.0.1:34848             ESTABLISHED 

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

[mapan@localhost test]$ netstat -na | grep 8888tcp        0      0 0.0.0.0:8888                0.0.0.0:*                   LISTEN      tcp        0      0 127.0.0.1:8888              127.0.0.1:34850             SYN_RECV    tcp        0      0 127.0.0.1:34846             127.0.0.1:8888              ESTABLISHED tcp        0      0 127.0.0.1:34848             127.0.0.1:8888              ESTABLISHED tcp        0      0 127.0.0.1:8888              127.0.0.1:34846             ESTABLISHED tcp        0      0 127.0.0.1:8888              127.0.0.1:34848             ESTABLISHED 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了。这个现象留给大家去实践一下吧,只有自己实践出来的东西才是自己的。











原创粉丝点击