有没有一种简单的方法可以用 java 程序重现 tcp CLOSE_WAIT 状态?
我有一个遗留的 Java 应用程序有这个问题,我希望能够重现它以便我可以测试我的修复。
谢谢
最佳答案
当另一端关闭连接而这一端没有关闭时,连接处于 CLOSE_WAIT 状态。很容易重现:
// Client.java (will sleep in CLOSE_WAIT)
import java.io.*;
import java.net.*;
public class Client
{
public static void main(String[] args) throws Exception
{
Socket socket = new Socket(InetAddress.getByName("localhost"), 12345);
InputStream in = socket.getInputStream();
int c;
while ((c = in.read()) != -1)
{
System.out.write(c);
}
System.out.flush();
// should now be in CLOSE_WAIT
Thread.sleep(Integer.MAX_VALUE);
}
}
// Server.java (sends some data and exits)
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String[] args) throws Exception
{
Socket socket = new ServerSocket(12345).accept();
OutputStream out = socket.getOutputStream();
out.write("Hello World\n".getBytes());
out.flush();
out.close();
}
}
关于java - 使用 java 客户端/服务器重现 tcp CLOSE_WAIT 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16077786/