当InputStream结束而没有显式断开时,HttpUrlConnection被关闭

时间:2017-10-08 07:32:39

标签: java httpurlconnection

在这段代码中,我试图读取对localhost服务器的不同连接的响应。 有趣的是,在我的代码中

  1. 只要我读完整个inputStream
  2. ,连接就会自动断开连接或释放资源
  3. 连接不会自动断开连接,我们必须显式关闭连接,以防我们不读取整个inputStream。这是预期的
  4. 此代码是否预期此行为(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 输出显然没有连接。

1 个答案:

答案 0 :(得分:0)

这是预期的行为。

如果没有额外收到的话,InputStream向服务器发送完成请求是不是很好?我没有看到它的问题。