What is the difference between calling setSoLinger with a value of 0 and not including soLinger?

In Java, is there a difference between setting a socket with Socket.setSoLinger (true, 0) and not calling setSoLinger at all? Is there any difference in how the socket closes?

I looked at Java sources and I see nothing special about closing sockets regarding soLinger. This makes me think this is a specific OS behavior. In my case, I am working on Linux.

+4
source share
2 answers

If the timeout is specified via setSoLinger() , close() blocked until the closed hand shake is completed or until the specified time has setSoLinger() .

However, close() does not indicate that the completion of the handshake is not completed, even if the time limit setSoLinger() expires before the completion of the final sequence.

In other words, setSoLinger() does not provide any additional guarantee for the application in the current Implementations.

Here is the source of information.

+1
source

Socket.setSoLinger(true, 0) actually the default, so there is no difference. However, see also Zaki's answer to the question of why even a nonzero value is in any case useless in Java. Socket.close() ignores errno , which is generated if the timeout expires. I noted this problem about ten years ago and received the answer that it cannot be changed for reasons of backward compatibility. In fact, I do not agree, but you go there. However, NIO2 stuff in Java 7 recognizes this case and explicitly throws an exception.

+1
source

Source: https://habr.com/ru/post/1400880/


All Articles