避免TIME_WAIT [英] Avoiding TIME_WAIT

查看:156
本文介绍了避免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.

由于我与同一主机/端口建立了许多连接,所以我最终开始看到使用中的地址"错误(请参阅

Since I make a number of connections to the same host / port, I eventually start seeing "Address in use" errors (see http://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 hostserver portclient 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_WAIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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