简体   繁体   English

accept()如何确定要返回的整数?

[英]How does accept( ) determine which integer to return?

Using the C sockets library on a Linux system... 在Linux系统上使用C套接字库...

When I make a call to accept( ) it always returns an integer. 当我调用accept( )它总是返回一个整数。 STDIN is 0. Usually my first accept call returns 3. They increment after that. STDIN为0。通常,我的第一个接受呼叫返回3。之后,它们递增。

I was wondering; 我在想; how does accept( ) determine which integer is next? accept( )如何确定下一个整数? If, after 2 more accept( ) calls, I have 3, 4, and 5 assigned to connected clients; 再经过2次accept( )调用后,如果我已将3、4和5分配给已连接的客户端; what happens when 4 disconnects? 4个断开连接时会发生什么? Is the next integer 4 or is it 6? 下一个整数是4还是6?

If someone could shed some light on this I sure would appreciate it. 如果有人可以对此有所了解,我肯定会感激的。

It uses the next currently unopen file descriptor, the same as open() and other system calls that return file descriptors; 它使用下一个当前未打开的文件描述符,与open()和其他返回文件描述符的系统调用相同。 dup2() is something of an exception to the pattern. dup2()是该模式的例外。 (A file descriptor might not be open but might still be unavailable for reuse if it was part of a network connection that has not been completely cleaned up yet, for example.) ( Update : struck out text reinstates original version of answer. If a file descriptor is closed, it is available for reuse. There might be issues with reusing a socket address because of FIN-WAIT states in TCP/IP - but a socket address is not the file descriptor.) (例如,如果文件描述符是尚未完全清理的网络连接的一部分,则可能无法打开文件描述符,但仍可能无法重用。)更新 :删除的文本会恢复答案的原始版本。文件描述符已关闭,可以重用。由于TCP / IP中的FIN-WAIT状态,重用套接字地址可能会出现问题-但套接字地址不是文件描述符。)

If you have descriptors 1-5 open, then close 4, the next open-like operation will return 4. 如果描述符1-5打开,然后关闭4,则下一个类似open的操作将返回4。

There might be security-conscious systems where this is not the pattern, but it is unlikely. 可能存在安全意识强的系统,虽然这不是这种模式,但不太可能。 One reason is that there is correct code for handling I/O redirection that relies on closing standard input (file descriptor 0) and the next open-like operation reusing the file descriptor; 原因之一是存在正确的代码来处理I / O重定向,该代码依赖于关闭标准输入(文件描述符0)以及下一个类似文件描述符的类似open的操作; repeat for standard output (file descriptor 1). 对标准输出(文件描述符1)重复上述步骤。

The short answer is, do NOT count on accept giving you integers in any expected order. 简短的答案是,不要指望接受以任何期望的顺序给您整数。 No matter how seductively easy you think it would make things if it did. 无论您认为它多么诱人,它都会成功。

What accept is returning is actually a 'descriptor' to a kernel resource that will connect that descriptor to the proper drivers necessary to read and write, close, seek(if capable). accept返回的实际上是内核资源的“描述符”,它将把该描述符连接到读写,关闭,查找(如果有能力)所需的适当驱动程序。 The pool of available descriptors is limited, so when you close a socket its descriptor goes back into the pool and can be reused. 可用描述符池是有限的,因此当您关闭套接字时,它的描述符会返回到池中并可以重用。

It's returning a file descriptor for the accepted socket: 它为接受的套接字返回文件描述符:

RETURN VALUES
     The call returns -1 on error and the global variable errno is set to
     indicate the error.  If it succeeds, it returns a non-negative integer
     that is a descriptor for the accepted socket.

This gives you a numeric value that What value that you can use with various file operations, and is decided behind the scenes. 这将为您提供一个数值,该数值可用于各种文件操作,并在幕后确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM