使用 SO_REUSEADDR - 以前打开的套接字会发生什么? [英] Using SO_REUSEADDR - What happens to previously open socket?

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

问题描述

在 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?

推荐答案

是的,当旧进程结束时,操作系统会自动关闭之前的套接字.您通常无法立即在同一端口上侦听的原因是,套接字虽然关闭,但仍会在一段时间(通常为几分钟)内保持 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 - 以前打开的套接字会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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