避免TIME

编程入门 行业动态 更新时间:2024-10-23 17:35:07
本文介绍了避免TIME_WAIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试避免客户端中的TIME_WAIT.我先连接,然后设置O_NONBLOCK和SO_REUSEADDR.我调用read直到它返回0.当read返回0时,errno也为0.我将此解释为服务器关闭连接的标志.但是,如果我调用close,则将套接字设置为TIME_WAIT,这已由netstat确认.

I'm trying to avoid TIME_WAIT in a client. I connect and then set O_NONBLOCK and SO_REUSEADDR. I call read until it returns 0. When read returns 0, the errno is also 0. I interpreted this as a sign that the server closed the connection. However, if I call close, the socket is set to TIME_WAIT, as confirmed by netstat.

由于我与同一主机/端口建立了许多连接,所以我最终开始看到使用中的地址"错误(请参阅 hea-www.harvard.edu/~fine/Tech/addrinuse.html ).

Since I make a number of connections to the same host / port, I eventually start seeing "Address in use" errors (see hea-www.harvard.edu/~fine/Tech/addrinuse.html).

读取返回0后,我应该调用close吗?如果我不这样做,文件描述符将被释放吗?

Should I be calling close after read returns 0? If I don't will the file descriptor be released?

推荐答案

启动关闭连接的那一面是处于TIME_WAIT状态的那一面. read()返回0应该表示服务器首先关闭了套接字,所以是的-这意味着TIME_WAIT最终出现在服务器端,并且客户端通过了LAST_ACK.

The side that is that one that initiated the closing of the connection is the one that ends up in the TIME_WAIT state. read() returning 0 is supposed to indicate that the server closed the socket first, so yes - this should mean that the TIME_WAIT ends up on the server side, and the client goes through LAST_ACK.

最终,您无法避免进入TIME_WAIT状态.即使您成功地将它从客户端移动到服务器端,您仍然不能重复使用(server host, server port, client host, client port)元组,直到TIME_WAIT结束(无论它在哪一侧).

At the end of the day, you can't avoid a TIME_WAIT state. Even if you succeed in moving it from the client to the server side, you still can't re-use that (server host, server port, client host, client port) tuple until the TIME_WAIT is over (regardless of which side it's on).

由于该元组的三个部分在您的方案中是固定的(server host,server port,client host),因此您实际上只有以下选项:

Since three parts of that tuple are fixed in your scenario (server host, server port, client host), you really only have these options:

  • 尝试提供更多客户端端口.默认情况下,某些操作系统仅将一小部分可用端口用于临时端口"(在这方面,我不确定OSX).如果是这种情况,请查看是否可以通过操作系统中的配置调整来更改范围,或者让应用程序循环搜索bind()/connect()的工作端口,直到连接正常为止.

  • Try to make more client ports available. Some operating systems only use a small range of the available ports for "ephemeral ports" by default (I'm not sure about OSX in this regard). If that's the case, see if you can change the range with a configuration tweak in the OS, or alternatively have the application hunt for a working port with bind()/connect() in a loop until the connection works.

通过在客户端上使用多个IP地址来扩展可用的client host值数量.不过,您必须将应用程序bind()专门用于这些IP地址之一.

Expand the number of client host values available, by using multiple IP addresses on your client. You'll have to have the application bind() to one of these IP addresses specifically though.

通过使用服务器上的多个端口和/或IP地址来扩展可用的server host/server port值的数量.客户端需要选择一个要连接的对象(轮询,随机等).

Expand the number of server host/server port values available, by using multiple ports and/or IP addresses on the server. The client will need to pick one to connect to (round robin, random, etc).

如果可行的话,这可能是最好的选择:重构协议,以使完成的连接不会关闭,而是进入空闲"状态,以便以后可以重新使用,而不必打开一个新的连接(例如HTTP保持活动状态).

Probably the best option, if it's doable: refactor your protocol so that connections that are finished aren't closed, but go into an "idle" state so they can be re-used later, instead of opening up a new connection (like HTTP keep-alive).

更多推荐

避免TIME

本文发布于:2023-11-13 00:05:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:TIME

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!