gpt4 book ai didi

java - HttpURL连接 : BindException when creating many connections

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:54:39 27 4
gpt4 key购买 nike

出于测试/基准测试目的,我想编写一个 Java 程序来循环执行以下任务:

  1. 通过 HTTP GET 从服务器加载数据
  2. (根据接收到的数据生成答案——此时不重要)
  3. 通过 HTTP POST 将答案发送到同一台服务器

这个循环同时在多个线程上运行。

启动后,程序可以在短时间内正常运行,并且每个线程每秒能够执行约 300 个循环(网络服务器在同一台机器上运行)。但 5-7 秒后,我得到了 BindException: Address already in use

在 20-30 秒的冷却时间后重新启动程序会导致相同的行为;当我不等待立即重新启动它时,它立即崩溃......所以我想这可能是绑定(bind)资源的问题。

这是一种使用 HttpURLConnection 的快速而肮脏的方法。相关部分:

从网络服务器获取数据

public String fetchData() throws IOException {

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setUseCaches(false);
conn.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}

in.close();
conn.disconnect();

return response.toString();
}

发送答案

public void sendData(byte[] data) throws IOException {

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setDoOutput(true);

OutputStream os = conn.getOutputStream();
os.write(data);
os.close();
conn.disconnect();
}

在线程内调用两种方法

@Override
public void run() {
while(true) {
try {
String data = fetchData();
String answer = // ... generating answer
sendData(answer.getBytes("UTF-8"));
} catch (IOException e) {
// ...
}
}
}

没有一个 URL 对象在线程之间共享 - 每个线程都有自己的 URL 实例(但是,每个实例都指向相同的地址)。

编辑:

这是发生的第一个异常的堆栈跟踪:

java.net.BindException: Address already in use: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.<init>(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at PayloadProcessor.fetchData(PayloadProcessor.java:66)
at PayloadProcessor.run(PayloadProcessor.java:32)
at java.lang.Thread.run(Unknown Source)

它出现在以下行的 fetchdata 方法中:

in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

我删除了对 disconnect() 的调用(正如 Aaron 所指出的)——不幸的是,同样的问题仍然存在。


让我们假设一切都正确关闭(不确定这里是否是这种情况,只是假设)- 程序是否太快了?我找到了另一个 post在 stackoverflow 上,“解决方案”是添加一个简单的 Thread.sleep - 但由于它是一个基准并且应该尽可能快地运行,我不喜欢那样。负载测试工具如何处理这个问题?


我将线程数减少到 1,但问题仍然出现。

最佳答案

听起来您的本地端口可能用完了。是否有可能并行生成了太多连接并且没有足够快地释放它们的资源以供其他线程重用?

我会摆脱对 disconnect() 的调用,因为这几乎会为您禁用连接池。来自文档:

Indicates that other requests to the server are unlikely in the near future. Calling disconnect() should not imply that this HttpURLConnection instance can be reused for other requests.

关于java - HttpURL连接 : BindException when creating many connections,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25964734/

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