我正在创建一个测试应用程序,以通过创建一系列出站https连接(每个都在其自己的线程中循环)来加载测试彗星servlet的过程。

并发出站http请求的数量是否有限制?如果是这样,是操作系统级别的问题(xp 32位)还是Java?当运行约100个连接时,出现以下异常。该错误不会立即发生,而是在50-150次循环之间(有时更多)之后发生,导致我怀疑实际上可能是我没有正确释放资源:

java.net.BindException: Address already in use: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:559)
    at         com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
 at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
 at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
 at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
 at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:272)
 at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:329)
 at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:172)
 at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:916)
 at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:158)
 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1177)
 at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
 at fqexconnectiontesting.FQEXHttpClient.run(FQEXHttpClient.java:158)
 at java.lang.Thread.run(Thread.java:662)

我的代码:
while(true){
try{
connection =(HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("keep-alive", "true");
connection.setRequestMethod("GET");
input = new DataInputStream( connection.getInputStream() );
b = new byte[4096];
totalBytes += input.read(b);
input.close();
b = null;
connection.disconnect();
connection = null;
Thread.sleep(100);
}

任何想法表示赞赏。

谢谢,
达兹

最佳答案

默认情况下,Java仅打开与同一主机的最多5个持久连接。
但是您的代码使用了connect()方法,该方法会阻止持久连接。

您最好阅读有关Persistent Connections的文档。将示例代码放在末尾以获取完全正常工作的持久连接。

也许这可以解决您的问题。

10-04 13:44