据我了解, linger 是一种设置,允许在关闭后使连接保持一些延迟,以便有时间读取最终数据。
我有这个例子:
@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();
}
}
}
和输出
1
2
2怎么读?逗留被禁用,从睡眠中醒来时套接字不应该处于不可读状态?