编辑:tl;博士:
不要用错误的选项打开插座,它不太可能工作!
原始问题:
我正在使用一个没有干净关闭机制的程序,它只是依赖于被终止它而被杀死。
它打开一个套接字如下:
(void) setsockopt(h, SOL_SOCKET, (SO_KEEPALIVE | SO_REUSEADDR), (int *) & optval, sizeof( optval ));重用地址选项似乎确实有效,因为最终套接字在程序死后会被释放。 最终成为从几秒钟到一刻钟的任何东西。
这非常繁琐,因为我需要定期重启这个程序。 使程序正确地发挥作用将是一项非常重要的工作,但我想知道我是否可以做更多的本地工作来更早地释放套接字?
EDIT: tl;dr:
Don't open sockets with the wrong options, it's unlikely to work!
Original Question:
I'm working with a program which doesn't have a clean shutdown mechanism, it just relies on being killed to terminate it.
It opens a socket as follows:
(void) setsockopt(h, SOL_SOCKET, (SO_KEEPALIVE | SO_REUSEADDR), (int *) & optval, sizeof( optval ));The re-use address option does seem to work, in that eventually the socket is released after the program dies. Eventually being anything from a couple of seconds up to maybe a minute.
This is quite tedious as I need to restart this program regularly. Making the program teminate properly would be a very big job, but I'm wondering if there's anything more local I can do to release the socket earlier?
最满意答案
套接字选项不是位,因此不能像SO_KEEPALIVE | SO_REUSEADDR一样 SO_KEEPALIVE | SO_REUSEADDR 。 要迂腐, SO_KEEPALIVE | SO_REUSEADDR SO_KEEPALIVE | SO_REUSEADDR产生另一个套接字选项SO_NO_CHECK 。
您没有注意到这个错误,因为您没有检查setsockopt的返回值。 现在你知道为什么忽略返回值被认为是不好的做法。
Socket options are not bits and hence can not be or'ed like SO_KEEPALIVE | SO_REUSEADDR. To be pedantic, SO_KEEPALIVE | SO_REUSEADDR yields another socket option SO_NO_CHECK.
You don't notice this mistake because you don't check the return value of setsockopt. Now you know why ignoring return values is considered bad practice.
当程序被杀时,Linux套接字释放得太慢(Linux socket released too slowly when program killed)编辑:tl;博士:
不要用错误的选项打开插座,它不太可能工作!
原始问题:
我正在使用一个没有干净关闭机制的程序,它只是依赖于被终止它而被杀死。
它打开一个套接字如下:
(void) setsockopt(h, SOL_SOCKET, (SO_KEEPALIVE | SO_REUSEADDR), (int *) & optval, sizeof( optval ));重用地址选项似乎确实有效,因为最终套接字在程序死后会被释放。 最终成为从几秒钟到一刻钟的任何东西。
这非常繁琐,因为我需要定期重启这个程序。 使程序正确地发挥作用将是一项非常重要的工作,但我想知道我是否可以做更多的本地工作来更早地释放套接字?
EDIT: tl;dr:
Don't open sockets with the wrong options, it's unlikely to work!
Original Question:
I'm working with a program which doesn't have a clean shutdown mechanism, it just relies on being killed to terminate it.
It opens a socket as follows:
(void) setsockopt(h, SOL_SOCKET, (SO_KEEPALIVE | SO_REUSEADDR), (int *) & optval, sizeof( optval ));The re-use address option does seem to work, in that eventually the socket is released after the program dies. Eventually being anything from a couple of seconds up to maybe a minute.
This is quite tedious as I need to restart this program regularly. Making the program teminate properly would be a very big job, but I'm wondering if there's anything more local I can do to release the socket earlier?
最满意答案
套接字选项不是位,因此不能像SO_KEEPALIVE | SO_REUSEADDR一样 SO_KEEPALIVE | SO_REUSEADDR 。 要迂腐, SO_KEEPALIVE | SO_REUSEADDR SO_KEEPALIVE | SO_REUSEADDR产生另一个套接字选项SO_NO_CHECK 。
您没有注意到这个错误,因为您没有检查setsockopt的返回值。 现在你知道为什么忽略返回值被认为是不好的做法。
Socket options are not bits and hence can not be or'ed like SO_KEEPALIVE | SO_REUSEADDR. To be pedantic, SO_KEEPALIVE | SO_REUSEADDR yields another socket option SO_NO_CHECK.
You don't notice this mistake because you don't check the return value of setsockopt. Now you know why ignoring return values is considered bad practice.
发布评论