当InputStream结束而没有明确的断开连接时,HttpUrlConnection被关闭

问题描述:

在这段代码中,我试图读取对本地主机服务器进行的不同连接的响应。 有趣的是,在我的代码当InputStream结束而没有明确的断开连接时,HttpUrlConnection被关闭

  1. 连接自动断开连接或因为我读了整个的inputStream
  2. 连接不会自动断开资源尽快释放,我们必须明确地关闭的情况下,连接我们不要读取整个inputStream。这是预期的

这是(1)行为预计从这段代码,如果是的话那么为什么?

下面是客户端的代码:

public class UrlConnectionHttpClient { 

    static List<InputStream> list = new ArrayList<InputStream>(); 

    public static void main(String[] args) throws InterruptedException { 
     System.out.println("time " + System.currentTimeMillis()); 
     for(int i = 0; i < 5; i++) { 
      call(i); 
     } 
    } 

    private static void read(InputStream in) throws IOException { 
     while (true) { 
      Integer a = in.read(); 
      if (a == -1) { 
       return; 
      } 
     } 
    } 

    static Thread call(final int i) throws InterruptedException { 
     Thread t = new Thread(new Runnable() { 
      public void run() { 
       try { 
        long t1 = System.currentTimeMillis(); 
        URL l = new URL("http://localhost:8020/?q=" + i); 
        HttpURLConnection connection = (HttpURLConnection) l.openConnection(); 
        System.out.println("starting to execute + " + i); 
        connection.setRequestMethod("POST"); 
        connection.setReadTimeout(1000 * 1000); 
        connection.setConnectTimeout(5 * 1000); 
        connection.setRequestProperty("Accept", "avro/binary"); 
        connection.setDoOutput(true); 
        InputStream inputStream = connection.getInputStream(); 
        read(inputStream); 
        //inputStream.close(); 
        //connection.disconnect(); 
        //list.add(connection.getInputStream()); 
        long t2 = System.currentTimeMillis(); 
        //System.out.println("finished to execute + " + i + " and time taken is: " + (t2 - ti) + " time is: " + System.currentTimeMillis() + " and size is: " + list.size()); 
        Thread.sleep(100000); 
        } catch (ProtocolException e) { 
         e.printStackTrace(); 
        } catch (MalformedURLException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
      } 
     }); 
     t.start(); 
     return t; 
    } 
} 

`

这是tcpdump的对服务器的端口的输出。 我们可以从tcpdump清楚地看到localhost服务器正在接收fin数据包。

12:26:46.079516 IP localhost.54880 > localhost.intu-ec-svcdisc: Flags [.], ack 65, win 12757, options [nop,nop,TS val 429721894 ecr 429721894], length 0 
12:26:46.079527 IP localhost.54880 > localhost.intu-ec-svcdisc: Flags [.], ack 9887, win 12450, options [nop,nop,TS val 429721894 ecr 429721894], length 0 
12:26:56.092276 IP localhost.54879 > localhost.intu-ec-svcdisc: Flags [F.], seq 119, ack 9887, win 12450, options [nop,nop,TS val 429731883 ecr 429721893], length 0 
12:26:56.092318 IP localhost.54881 > localhost.intu-ec-svcdisc: Flags [F.], seq 119, ack 9887, win 12450, options [nop,nop,TS val 429731883 ecr 429721893], length 0 
12:26:56.092333 IP localhost.54878 > localhost.intu-ec-svcdisc: Flags [F.], seq 119, ack 9887, win 12450, options [nop,nop,TS val 429731883 ecr 429721894], length 0 
12:26:56.092339 IP localhost.intu-ec-svcdisc > localhost.54879: Flags [.], ack 120, win 12755, options [nop,nop,TS val 429731883 ecr 429731883], length 0 
12:26:56.092350 IP localhost.54877 > localhost.intu-ec-svcdisc: Flags [F.], seq 119, ack 9887, win 12450, options [nop,nop,TS val 429731883 ecr 429721893], length 0 
12:26:56.092353 IP localhost.intu-ec-svcdisc > localhost.54881: Flags [.], ack 120, win 12755, options [nop,nop,TS val 429731883 ecr 429731883], length 0 
12:26:56.092362 IP localhost.intu-ec-svcdisc > localhost.54878: Flags [.], ack 120, win 12755, options [nop,nop,TS val 429731883 ecr 429731883], length 0 
12:26:56.092363 IP localhost.54880 > localhost.intu-ec-svcdisc: Flags [F.], seq 119, ack 9887, win 12450, options [nop,nop,TS val 429731883 ecr 429721894], length 0 
12:26:56.092379 IP localhost.intu-ec-svcdisc > localhost.54877: Flags [.], ack 120, win 12755, options [nop,nop,TS val 429731883 ecr 429731883], length 0 
12:26:56.092390 IP localhost.intu-ec-svcdisc > localhost.54880: Flags [.], ack 120, win 12755, options [nop,nop,TS val 429731883 ecr 429731883], length 0 
12:26:56.094884 IP localhost.intu-ec-svcdisc > localhost.54877: Flags [F.], seq 9887, ack 120, win 12755, options [nop,nop,TS val 429731885 ecr 429731883], length 0 
12:26:56.095051 IP localhost.54877 > localhost.intu-ec-svcdisc: Flags [.], ack 9888, win 12450, options [nop,nop,TS val 429731885 ecr 429731885], length 0 
12:26:56.095483 IP localhost.intu-ec-svcdisc > localhost.54878: Flags [F.], seq 9887, ack 120, win 12755, options [nop,nop,TS val 429731885 ecr 429731883], length 0 
12:26:56.095561 IP localhost.54878 > localhost.intu-ec-svcdisc: Flags [.], ack 9888, win 12450, options [nop,nop,TS val 429731885 ecr 429731885], length 0 
12:26:56.095935 IP localhost.intu-ec-svcdisc > localhost.54881: Flags [F.], seq 9887, ack 120, win 12755, options [nop,nop,TS val 429731886 ecr 429731883], length 0 
12:26:56.095984 IP localhost.54881 > localhost.intu-ec-svcdisc: Flags [.], ack 9888, win 12450, options [nop,nop,TS val 429731886 ecr 429731886], length 0 
12:26:56.096413 IP localhost.intu-ec-svcdisc > localhost.54879: Flags [F.], seq 9887, ack 120, win 12755, options [nop,nop,TS val 429731886 ecr 429731883], length 0 
12:26:56.096474 IP localhost.54879 > localhost.intu-ec-svcdisc: Flags [.], ack 9888, win 12450, options [nop,nop,TS val 429731886 ecr 429731886], length 0 
12:26:56.096873 IP localhost.intu-ec-svcdisc > localhost.54880: Flags [F.], seq 9887, ack 120, win 12755, options [nop,nop,TS val 429731886 ecr 429731883], length 0 
12:26:56.096936 IP localhost.54880 > localhost.intu-ec-svcdisc: Flags [.], ack 9888, win 12450, options [nop,nop,TS val 429731886 ecr 429731886], length 0 

而且从lsof的输出很明显,没有任何连接。

这是预期的行为。

InputStream发送完成请求到服务器是否好,如果没有额外的东西要接收?我没有看到它的问题。

+0

这显然是好的。但是我所见过的每一个文档都明确指定关闭inputStream和HttpurlConnection。 –

+0

尽快释放资源是件好事。否则,它将在稍后由垃圾收集器完成。 –

+0

正如我上面所说,释放资源显然是很好的。目前在我上面的摘录中,它不是由GC完成的,因为我在我的代码中有一个Thread.Sleep,并且在此期间已经执行了tcpdump。 –