我对积压的监听系统调用和套接字编程中的连接数的理解是否正确? [英] Is my understanding right on the listen system call on backlog and the number of connections in socket programming?

查看:30
本文介绍了我对积压的监听系统调用和套接字编程中的连接数的理解是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 每个连接 - 服务器(唯一 IP 地址 + 主机号)+ 客户端.这是一种独特的连接组合.
  2. 可以根据操作系统支持的文件描述符数量来建立这种组合.例如,我的机器有以下限制 -

  1. Every connection - Server ( unique IP address + host number) + client. This is a unique combination of a connection.
  2. Such combinations can be established based on the number of file descriptors supported by the OS. For instance, my machine has the following limit -

cat/proc/sys/fs/file-max380594

cat /proc/sys/fs/file-max 380594

当我们说 listen(sock_fd, 5);这里 5 表示不会被拒绝连接的连接数.事实上,它们将被置于挂起状态.在这里,让我们假设所有 fd 都用于套接字连接.然后第 389595 个到第 389599 个连接将进入挂起状态.一旦 fd 可用,这将被处理.我说的对吗?

When we say listen(sock_fd, 5); Here 5 means the number of connections who will not be denied connections. In fact, they will be put into a pending state. Here, let's assume all the fd's are for the socket connection. Then the 389595th till 389599th connection will be put into a pending state. This will be handled once the fd are available. Am I correct on it?

推荐答案

不太清楚.

Re (1),我不知道您所说的主机号"是什么意思.一个客户端可以有两个到给定服务器的连接,只是源端口不同.因此,要唯一标识一个连接,您需要客户端 IP 和端口号二元组(对),以及服务器 IP 和端口号二元组.

Re (1), I don't know what you mean by 'host number'. A client can have two connections to a given server differing only by source port. So to uniquely identify a connection, you need the client IP and port number 2-tuple (pair), and the server IP and port-number 2-tuple.

Re (2),这里有几个限制.每个打开的连接都需要一个文件描述符,但事情并没有那么简单.例如,TIME_WAIT 中的连接可能已经关闭了它们的文件描述符,但从操作系统的角度来看,它们仍然是一个连接",因为它们正是将杂散连接数据包与之关联的.此外,文件数量可能存在总体限制,但还会有其他限制.例如,每个进程的打开 fd 数量是有限制的(参见 getrlimit).这个问题之前已经被问过几次 - 见 现代 Linux 机器可以拥有的理论最大开放 TCP 连接数是多少增加linux中最大tcp/ip连接数

Re (2), there are several limits at play here. Each open connection requires a file descriptor, but life is not that simple. Connections in TIME_WAIT for instance may have had their file descriptor closed, but are still a 'connection' from the OS point of view in that they are their precisely to associate stray connection packets with. Further, there may well be an overall limit to the number of files, but there will be other limits. For instance, there is a limit to the number of open fds per process (see getrlimit). This question has been asked several times before - see What is the theoretical maximum number of open TCP connections that a modern Linux box can have and Increasing the maximum number of tcp/ip connections in linux

Re (3),不,backlog 不会像那样工作,因为它是每个套接字都处于侦听状态.积压数量表示侦听套接字将确认的尚未接受的 TCP 连接的最大数量.当一个新的 TCP 连接试图在给定的侦听套接字上进行连接时,操作系统必须将该挂起的连接的存在排队,直到它使用 accept 传输到一个新的套接字(以处理特定的连接).积压数量只是该队列的最大深度.这与 file-max 无关,除非系统上的每个 FD 都被一个监听单个套接字的进程使用(这种情况不太可能发生).

Re (3), no, the backlog does not work like that as it is per socket in a listen state. The backlog number indicates the maximum number of TCP connection that the listening socket will acknowledge that have not yet been accepted. When a new TCP connection attempts to connect on a given listening socket, the OS has to queue the existence of that pending connection until it is transferred to a new socket (to handle the particular connection) using accept. The backlog number is simply the maximum depth of that queue. This has nothing to do with file-max unless every FD on the system was used by a single process listening on a single socket (a rather unlikely scenario).

这篇关于我对积压的监听系统调用和套接字编程中的连接数的理解是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆