listen函数

  • 0.往期回顾 (用心写好每一篇文章)
  • 1.listen函数查看方法
  • 2.详细解说(中文)
    • listen:
  • 3.案例分析
    • 运行截图
    • 案例代码
  • 4.socket文档

0.往期回顾 (用心写好每一篇文章)

1.深入了解socket函数
2.深入了解bind函数

1.listen函数查看方法

使用指令:man listen


man 文档

2.详细解说(中文)

listen:

1.功能:
listen函数使用主动连接套接字变为被连接套接口,使得一个进程可以接受其它进程的请求,从而成为一个服务器进程。在TCP服务器编程中listen函数把进程变为一个服务器,并指定相应的套接字变为被动连接。

调用listen导致套接字从CLOSED状态转换到LISTEN状态。

2.函数原型:

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>int listen(int sockfd, int backlog);

3.参数说明:

1.sockfd:一个已绑定未被连接的套接字描述符
2.backlog: 规定了内核应该为相应套接字排队的最大连接个数。用SOMAXCONN则为系统给出的最大值

为了理解其中的backlog参数,我们必须认识到内核为任何一个给定的监听套接字维护两个队列:
(1)未完成连接队列。每个这样的SYN分节对应其中一项:已由某个客户发出并到达服务器,而服务器正在等待完成相应的TCP三路握手过程。这些套接字处于SYN_ RCVD状态
(2)已完成连接队列。每个已完成TCP三路握手过程的客户对应其中一项,这些套接字处于ESTABLISHED状态。

每当在未完成连接队列中创建一项时,来自监听套接字的参数就复制到即将建立的连接中。连接的创建机制是完全自动的,无需服务器进程插手。

下图所列的各种操作系统下,backlog参数取不同值时已排队连接的实际数
目。7个操作系统被归纳成5列不同的值,可见对backlog的意义的解释是如此多样。

4.函数返回值:

成功: 0
失败:-1 并设置errno

5.其他参考解释:百度百科

百度百科·listen函数

3.案例分析

运行截图

listen函数调用成功

案例代码

#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>typedef struct sockaddr SA;void init()
{printf("server is start\n");//创建sockedint sockfd = socket(PF_INET,SOCK_STREAM,0);if(-1 == sockfd){perror("socket error\n");printf("server not start\n");exit(-1);}printf("socket create OK\n");//通信地址struct sockaddr_in addr;addr.sin_family = PF_INET;addr.sin_port = htons(10086);addr.sin_addr.s_addr = inet_addr("127.0.0.1");//绑定端口int bin = bind(sockfd,(SA*)&addr,sizeof(addr));if(-1 == bin){perror("bind error\n");printf("server not strat\n");exit(-1);}printf("bind port OK\n");//设置监听int lst =listen(sockfd,100);if(-1 == lst){perror("falt listen\n");printf("server not start\n");exit(-1);}printf("listen seccess!\n");}int main()
{init();return 0;}

4.socket文档

LISTEN(2)                                                           Linux Programmer's Manual                                                          LISTEN(2)NAMElisten - listen for connections on a socketSYNOPSIS#include <sys/types.h>          /* See NOTES */#include <sys/socket.h>int listen(int sockfd, int backlog);DESCRIPTIONlisten()  marks the socket referred to by sockfd as a passive socket, that is, as a socket that will be used to accept incoming connection requests usingaccept(2).翻译:listen()将sockfd引用的套接字标记为被动套接字,也就是说,该套接字将用于使用accept()接受传入的连接请求。The sockfd argument is a file descriptor that refers to a socket of type SOCK_STREAM or SOCK_SEQPACKET.翻译:sockfd参数是一个文件描述符,它引用类型为SOCK_STREAM或SOCK_SEQPACKET的套接字。The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow.  If a connection request arrives when  thequeue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request maybe ignored so that a later reattempt at connection succeeds.翻译:backlog参数定义sockfd的挂起连接队列可能增长到的最大长度。如果连接请求在队列已满时到达,则客户端可能会收到一个错误,并指示ECONREFUNCE,或者,如果基础协议支持重新传输,则该请求可能会被忽略,以便稍后重新尝试连接成功。RETURN VALUEOn success, zero is returned.  On error, -1 is returned, and errno is set appropriately.翻译:成功时,返回0。在出现错误时,返回-1,并正确设置errno。ERRORSEADDRINUSEAnother socket is already listening on the same port.翻译:另一个套接字已在同一端口上侦听。EADDRINUSE(Internet domain sockets) The socket referred to by sockfd had not previously been bound to an address and, upon  attempting  to  bind  it  to  anephemeral  port,  it  was  determined  that  all  port  numbers  in  the  ephemeral  port  range  are  currently  in  use.   See the discussion of/proc/sys/net/ipv4/ip_local_port_range in ip(7).翻译:(Internet域套接字)sockfd引用的套接字以前未绑定到地址,在尝试将其绑定到临时端口时,确定临时端口范围内的所有端口号当前都在使用中。参见对/proc/sys/net/ipv4/ip\本地\端口\范围在ip(7)中。EBADF  The argument sockfd is not a valid file descriptor.翻译:参数sockfd不是有效的文件描述符。ENOTSOCKThe file descriptor sockfd does not refer to a socket.翻译:文件描述符sockfd不引用套接字。EOPNOTSUPPThe socket is not of a type that supports the listen() operation.翻译:套接字的类型不支持listen()操作
CONFORMING TOPOSIX.1-2001, POSIX.1-2008, 4.4BSD (listen() first appeared in 4.2BSD).NOTESTo accept connections, the following steps are performed:翻译:要接受连接,请执行以下步骤1.  A socket is created with socket(2).翻译:使用socket创建套接字。2.  The socket is bound to a local address using bind(2), so that other sockets may be connect(2)ed to it.翻译:使用bind将套接字绑定到本地地址,以便其他套接字可以connect到它。3.  A willingness to accept incoming connections and a queue limit for incoming connections are specified with listen().翻译:使用listen()指定接受传入连接的意愿和传入连接的队列限制。4.  Connections are accepted with accept(2)翻译:使用accept接受连接POSIX.1 does not require the inclusion of <sys/types.h>, and this header file is not required on Linux.  However, some historical  (BSD)  implementationsrequired this header file, and portable applications are probably wise to include it.翻译:不需要包含<sys/类型。h> ,并且Linux上不需要此头文件。但是,一些历史(BSD)实现需要此头文件,而便携式应用程序可能明智地将其包括在内。The behavior of the backlog argument on TCP sockets changed with Linux 2.2.  Now it specifies the queue length for completely established sockets waitingto be accepted, instead of the number of incomplete connection requests.  The maximum length of the  queue  for  incomplete  sockets  can  be  set  using/proc/sys/net/ipv4/tcp_max_syn_backlog.  When syncookies are enabled there is no logical maximum length and this setting is ignored.  See tcp(7) for moreinformation.If the backlog argument is greater than the value in /proc/sys/net/core/somaxconn, then it is silently truncated to that value.  Since Linux 5.4, the de‐fault  in  this file is 4096; in earlier kernels, the default value is 128.  In kernels before 2.4.25, this limit was a hard coded value, SOMAXCONN, withthe value 128.EXAMPLESee bind(2).SEE ALSOaccept(2), bind(2), connect(2), socket(2), socket(7)COLOPHONThis page is part of release 5.05 of the Linux man-pages project.  A description of the project, information about reporting bugs, and the latest versionof this page, can be found at https://www.kernel.org/doc/man-pages/.Linux                                                                      2020-02-09  

参考文档
[1] linux man手册
[2]《Linux高性能服务器》
[3]《Unix网络编程卷1》




3.深入了解listen函数相关推荐

  1. C语言网络编程:listen函数详解

    文章目录 前言 函数描述 代码实例 TCP服务器为什么调用listen 前言 根据TCP编程模型中我们可以看到之前的socket和bind接口是tcp服务器在为接收客户端的链接做准备,保证tcp的面向 ...

  2. listen函数介绍

    文章目录 1 listen函数介绍 1 listen函数介绍 #include <sys/types.h> /* See NOTES */ #include <sys/socket. ...

  3. listen()函数中backlog参数分析

    背景知识 Unix网络编程描述如下: 总结 0. accept()函数不参与三次握手,而只负责从已建立连接队列中取出一个连接和sockfd进行绑定: 1. backlog参数决定了未完成队列和已完成队 ...

  4. C语言 socket listen()函数(socket()函数创建的socket(套接字描述符)默认是一个主动类型的,listen函数将socket变为被动类型的,等待客户的连接请求)

    摘要:listen函数使用主动连接套接口变为被连接套接口,使得一个进程可以接受其它进程的请求,从而成为一个服务器进程.在TCP服务器编程中listen函数把进程变为一个服务器,并指定相应的套接字变为被 ...

  5. listen函数与海量用户同时登陆

    1. 初步怀疑服务器监听能力与Listen函数有关系, 把Socket设置为Listen(1) ---50个相同用户名字同时登陆 使用脚本@start DlgTcpClient.exe进行 第一次测试 ...

  6. linux网络编程之Listen函数参数介绍

    1.listen()函数介绍 listen函数使用主动连接套接口变为被连接套接口,使得一个进程可以接受其它进程的请求,从而成为一个服务器进程.在TCP服务器编程中listen函数把进程变为一个服务器, ...

  7. LwIP tcp/ip socket编程listen函数分析

    函数原型为: [cpp] view plain copy int listen(int  sockfd, int  backlog); <span style="font-family ...

  8. 网络编程之 listen()函数的使用与三次握手的理解

    listen()函数 在进入我们的函数讲解前大家再回顾一下编写服务器端的流程 服务器端:socket()-->bind( )-->listen()-->accept()-->r ...

  9. listen()函数中的SOMAXCONN含义

    导读:在linux中,/proc/sys/net/core/somaxconn这个参数,linux中内核的一个不错的参数somaxconn 看下其解析: 对于一个TCP连接,Server与Client ...

  10. listen函数的第二个参数_signal(SIGPIPE,?SIG_IGN)listen函数中backlog参数分析

    signal(SIGPIPE, SIG_IGN); TCP是全双工的信道, 可以看作两条单工信道, TCP连接两端的两个端点各负责一条. 当对端调用close时, 虽然本意是关闭整个两条信道, 但本端 ...

最新文章

  1. html中选择样式,html中css三种常见的样式选择器 zz
  2. Activity生命周期的补充
  3. promise的大白话讲解
  4. CSS 单行溢出文本显示省略号...的方法(兼容IE FF)(转)
  5. 前端开发的难点到底在什么地方?
  6. Could not load dynamic library ‘libcudart.so.10.0‘; dlerror: libcudart.so.10.0: cannot open shared o
  7. [设计模式-行为型]迭代器模式(Iterator)
  8. OkHttp文件上传下载
  9. python中__init__()、__new__()、__call__()、__del__()几个魔法方法的用法
  10. Java API For WebSocket(七)Java EE环境
  11. JAVA网络编程实战(笔记)
  12. 重磅 | 完备的人工智能AI 学习——基础知识学习路线,所有资料免关注免套路直接网盘下载
  13. Idea编译提示Java找不到符号解决方式
  14. Androd Camera Yuv Jepg bmp
  15. android中倒计时动画,简单实现Android倒计时效果
  16. linux下载测序数据,高速下载测序数据(SRA,Fastq等)
  17. 在mips64架构的国产系统中安装pyinstaller
  18. 云上部署oracle rac,在青云上部署oracle rac全过程
  19. NTC电阻在电源输入端的应用-测试案例
  20. 主流电源的调光协议分析及应用说明

热门文章

  1. vscode终端清屏
  2. 一点浩然气,千里快哉风(修炼孟子浩然之气)
  3. FDTD Solutions初学笔记
  4. 参考文献外国名字写法
  5. 单树莓派/双树莓派+USRP+srsLTE分布式搭建4G LTE微基站
  6. aes ccm模式 java_AES_GCM和AES_CCM的选择
  7. 美国密歇根州立大学计算机专业,密歇根州立大学计算机科学与工程系开设的研究领域有哪些?...
  8. android人脸抠图,人脸框抠图如何实现
  9. 使用鼠标右击Gite Bash Here 将本地文件上传到 GitHub
  10. DNS的作用是什么?为什么一定要配置DNS才能上网