在java中测试TCP Keepalive

时间:2018-06-06 10:51:17

标签: java tcp-keepalive

我试图在java中测试TCP Keepalive参数, KEEPALIVE_IDLE_TIME,KEEPALIVE_PROBE_COUNT,KEEPALIVE_INTERVAL_TIME由OS设置和控制。这三个可以通过线程属性设置。但是我无法通过独立程序重现死连接或空闲套接字的问题(即使在使用thread.sleep之后)。以下是代码段:

public Server(int port)
   {    
          ThreadProperties.setProperty(ThreadProperties.KEEPALIVE_IDLE_TIME,"300" );
ThreadProperties.setProperty(ThreadProperties.KEEPALIVE_PROBE_COUNT, "5");          ThreadProperties.setProperty(ThreadProperties.KEEPALIVE_INTERVAL_TIME,"5");

       try
       {
           server = new ServerSocket(port);
           socket= new Socket("127.0.0.1",6001);
           socket.setKeepAlive(true);
           socket = server.accept();
           try {
                Thread.sleep(600000); //wait for 10 min
            } catch (InterruptedException e) {
                e.printStackTrace();
            }        
              OutputStream outstream = socket.getOutputStream();
              PrintWriter out = new PrintWriter(outstream);
              String toSend = "String to send to server";
              out.write(toSend );
           socket.close();
       }
       catch(IOException i)
       {
           System.out.println(i);
       }
   }

理想情况下,套接字应该在out.write(toSend)步骤中空闲并失败。但它仍然处于既定状态并且没有任何例外地关闭。如何在java中测试这个用例?

0 个答案:

没有答案