使用SO_REUSEADDR - 恰好previously打开套接字是什么? [英] Using SO_REUSEADDR - What happens to previously open socket?

查看:115
本文介绍了使用SO_REUSEADDR - 恰好previously打开套接字是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在UNIX网络编程,我总是将插座上的SO_REUSEADDR选项正在由服务器来听上的连接。这基本上说,另一座可在机器上的同一个端口上打开。从崩溃中恢复和插座没有正确关闭时,这是非常有用的 - 应用程序可以重新启动,这将只需打开同一个端口上的另一个插座,并继续收听

In network programming in unix, I have always set the SO_REUSEADDR option on the socket being used by server to listen to connections on. This basically says that another socket can be opened on the same port on the machine. This is useful when recovering from a crash and the socket was not properly closed - the app can be restarted and it will simply open another socket on the same port and continue listening.

我的问题是,会发生什么老插座?毫无疑问,所有的数据/连接仍然会在旧插座好评。是否得到由操作系统自动关闭?

My question is, what happens to the old socket? Without a doubt, all data/connections will still be received on the old socket. Does it get closed automatically by the OS?

推荐答案

是的,OS的时候,老进程结束自动关闭previous插座。您无法正常在同一端口上侦听马上的原因是因为插座,虽然封闭,保持在2MSL状态一定时间(一般几分钟)。操作系统超时到期时会自动转换旧插座脱离这种状态。

Yes, the OS automatically closes the previous socket when the old process ends. The reason you can't normally listen on the same port right away is because the socket, though closed, remains in the 2MSL state for some amount of time (generally a few minutes). The OS automatically transitions the old socket out of this state when the timeout expires.

这篇关于使用SO_REUSEADDR - 恰好previously打开套接字是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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