gpt4 book ai didi

java - 当 InputStream 结束而没有显式断开连接时,HttpUrlConnection 会关闭

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:53 25 4
gpt4 key购买 nike

在这段代码中,我试图读取与本地主机服务器建立的不同连接的响应。有趣的是,在我的代码中

  1. 一旦我读取整个输入流,连接就会自动断开或资源被释放
  2. 连接不会自动断开,我们必须显式关闭连接,以防我们没有读取整个输入流。这是预期的

此代码是否预期出现此 (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 向服务器发送完成请求不是很好吗?我不认为它有什么问题。

关于java - 当 InputStream 结束而没有显式断开连接时,HttpUrlConnection 会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46628575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com