听套接字多次调用-预期的行为? [英] listen called on socket more than once -- expected behavior?

查看:36
本文介绍了听套接字多次调用-预期的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用基于C的简单服务器时,我发现有些奇怪我的Linux(4.10.3)系统上的程序.我不小心结束打电话了我在套接字上(从服务器进程中)两次 listen()之前称为 bind().我注意到两个监听电话成功,没有任何错误.实际上,似乎多少并不重要我打来的电话,所有听的电话都成功.

I noticed something odd while working with a simple C-based server program on my Linux (4.10.3) system. I accidentally ended up calling listen() twice on a socket (from the server process) I had called bind() on earlier. I noticed that both the listen calls succeeded without any errors. In fact, it doesn't seem to matter how many calls to listen I make, all calls to listen succeed.

我希望所有呼叫在第一个失败后都能监听使用 EADDRINUSE .我想念什么吗?Linux/POSIX手册页没有似乎对此无话可说.我确实找到了一个关于以下网页上的这种行为:

I was expecting all the calls to listen after the first one to fail with EADDRINUSE. Am I missing something? Linux/POSIX man pages don't seem to say anything about this. I did find one reference about this behavior on the following web-page:

https://www.mkssoftware.com/docs/man3/listen.3.asp

应用程序可能在同一套接字上多次调用listen().这具有为侦听更新当前积压的效果插座.是否有比新的待办事项更多的挂起连接值,多余的未决连接将被重置并丢弃.

An application may call listen() more than once on the same socket. This has the effect of updating the current backlog for the listening socket. Should there be more pending connections than the new backlog value, the excess pending connections are reset and dropped.

我不清楚这是否也适用于Linux.所以我的问题是:当在同一套接字上多次调用listen时会发生什么情况同样的过程?(显然,用户空间中没有明显的副作用,但是Linux内核在内核空间上有什么特殊之处吗?)

It's unclear to me if this also applies to Linux. And so my question is: what happens when listen is called on the same socket more than once from the same process? (Apparently there are no visible side-effects in user space, but does the Linux kernel do something special in kernel space?)

谢谢.

推荐答案

Linux内核使用新的积压值来调整积压队列长度,但仅用于将来的连接请求.它不会丢弃队列中已经存在的任何挂起的连接.

The Linux kernel adjusts the backlog queue length using the new backlog value, but only for future connection requests. It does not discard any pending connections already in the queue.

listen()的第二次和后续调用没有其他作用,除非套接字的类型错误(不是 SOCK_STREAM )或套接字位于其中,否则不会失败.错误的状态(已经连接到特定的对等设备,或者已经处于关闭状态).

Second and subsequent calls to listen() have no other effect, and will not fail unless the socket is of the wrong type (not SOCK_STREAM), or is in the wrong state (already connected to a specific peer, or is already in the process of being closed).

这篇关于听套接字多次调用-预期的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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