From what I understand, linger is a setting allowing to keep the connection some delay after close for the final data to have time to be read.
I have this example:
@Ignore
public class SocketTTest
{
@Test
public void socketTest() throws Throwable
{
new Thread(() -> {
try
{
ServerSocket serverSocket = new ServerSocket(5555);
while(true)
{
//keep listening
Socket socket = serverSocket.accept();
socket.setSoLinger(false, 0);
InputStream in = socket.getInputStream();
System.out.println(in.read());
Thread.sleep(2000);
System.out.println(in.read());
in.close();
socket.close();
System.exit(0);
}
}
catch(Throwable ex)
{
ex.printStackTrace();
}
}).start();
Socket socket = new Socket("localhost", 5555);
socket.setSoLinger(false, 0);
OutputStream out = socket.getOutputStream();
out.write(1);
out.write(2);
out.close();
socket.close();
synchronized(this)
{
wait();
}
}
}
And the output
1
2
How does the 2 is read? The linger is disabled, the socket shouldn't be in a unreadable state when coming out of sleep?