How to set SO_LINGER socket option to ServerSocket? Should i extend SocketImpl class. But there are many abstract methods in SocketImpl class.
Asked
Active
Viewed 1,367 times
2 Answers
1
Set it on the Socket
that you get from serverSocket.accept()
.

Kayaman
- 72,141
- 5
- 83
- 121
-
Hi Kayaman, i think that will not modify the serversocket's SO_LINGER setting. – Praveen Kumar Nov 24 '14 at 07:08
-
1@PraveenKumar Of course not, since the `ServerSocket` doesn't have such a setting. You could set your own `SocketFactory` that automatically does `setSoLinger()` on the sockets it returns. But even then, you cab't set it on the `ServerSocket` (it doesn't make sense). – Kayaman Nov 24 '14 at 07:23
1
You can't set SO_LINGER
on a ServerSocket
. It doesn't make sense.
I'm wondering whether you even know what SO_LINGER
is for. It is a very rarely used technique for enforcing a close timeout on connected TCP sockets, or, unfortunately, for forcing them to reset the connection when closed, which is an operation by far best avoided for many reasons, much-abused, and little understood.
You almost certainly don't want to use it at all on any socket whatsoever.

user207421
- 305,947
- 44
- 307
- 483
-
Hi all, actually my problem is: Java process will create a server socket and it listens on a random port in **0.0.0.0** and **::** and then it spawns mysql process. mysql has nothing to do with that serversocket. That serversocket is meant to be used by another java process. Sometimes due to outofmemory like error java process will crash. Mysql will continue to run, but the serversocket continues to listen in **::** (it is in listening state, BUT IT IS NOT LISTENING ON 0.0.0.0). Why the serversocket continues to listen (only in ipv6 interface) even though java got killed. – Praveen Kumar Nov 25 '14 at 07:01
-
So i am trying various ways to force close the server socket if java got killed. – Praveen Kumar Nov 25 '14 at 07:05
-
Point to be noted is, if i kill mysql process, that server socket stops to listen. But there is no relation between the serversocket and mysql process. – Praveen Kumar Nov 25 '14 at 07:11
-
@PraveenKumar Yes there is. You stated in [your other thread](http://stackoverflow.com/q/26888395/207421) that 'my application starts mysql database'. The mysql process is therefore a child process of the Java process. That's the strongest inter-process relationship there is. Why you're using Java to control MySQL is another question. In any case you can't set any `SO_LINGER` state on sockets in the LISTENING state, in any language, and even if you could it wouldn't solve this problem. You're barking up the wrong tree completely. – user207421 Nov 26 '14 at 04:22