java.net.SocketException: No buffer space available (maximum connections reached?): connect(java.net.SocketException:没有可用的缓冲区空间(达到最大连接数?):连接)
问题描述
您好,我正在使用 Apache HTTP Client 4.0 在基于 HTTPS 协议的服务器上上传一些文件.上传的应用程序运行 24x7.今天突然开始抛出这个异常-
Hi I am using Apache HTTP Client 4.0 to upload some files on a server based on HTTPS protocol. The uploaded application is running 24x7. Today suddenly it started to throw this exception-
java.net.SocketException: No buffer space available (maximum connections reached?): connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:333)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:123)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:147)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:101)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:381)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)
谁能帮帮我?我完全不知道发生了什么?
Can anyone please help me? I am totally clueless on what is going on?
这是上传文件的源代码-
This is the source code which upload the file -
public File call() throws Exception {
HttpClient httpclient = new DefaultHttpClient();
try{
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
/*
* Create POST REQUEST
*/
HttpPost httpPost = new HttpPost(this.URL);
/*
* Create MultipartRequestEntity
*/
MultipartEntity multipartEntity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
/*
* Add POST Parameters
*/
multipartEntity.addPart(parameters[0], this.fileBody);
multipartEntity.addPart(parameters[1], new StringBody(this.TYPE));
multipartEntity.addPart(parameters[2], new StringBody(this.MAAID));
/*
* Add this POST Method
*/
httpPost.setEntity(multipartEntity);
/*
* Upload the file
*/
HttpResponse response = httpclient.execute(httpPost);
int responseCode = response.getStatusLine().getStatusCode();
logger.info("Response Code of HTTP Connectivity ["+ responseCode + "], " +
"it means ["+ response.getStatusLine().getReasonPhrase()+"]");
/*
* Check the server Response
*/
HttpEntity entity = response.getEntity();
if(entity != null){
String status = EntityUtils.toString(entity);
logger.info("Status of file upload from Server >>"+ status+"<<");
entity.consumeContent();
if(status.equalsIgnoreCase("OK")){
return this.fileBody.getFile();
}
}else{
logger.error("Unable to retrieve status of file upload from server");
}
}catch(NoRouteToHostException e){
logger.error("Internet connection to ["+ this.URL + "] is not available", e);
}catch(SocketException e){
logger.error("Unable to connect to "+ this.URL, e);
}catch (Exception e) {
logger.error("Exception while uploading the file["+ this.fileBody.getFilename()+ "] on ["+ this.URL+"]", e);
}finally{
try{
httpclient.getConnectionManager().shutdown();
}catch(Exception e){
// Ignore this exception
}
}
return null;
}
推荐答案
我的猜测:您的端口用完了,问题与您的代码没有直接关系,而是与服务器的当前状态有关.与其他计算机打开的连接过多,这会导致问题.
My guess: you are running out of ports and the issue isn't directly related to your code but to the current state of your server. Too many connections are opened to other machines and this causes the issue.
寻找什么:
- 您的服务器是否处于可能导致打开多个网络连接的大量使用?
- HTTP 客户端文档 建议只实例化一个
HttpClient
并重用这个实例.它们是案例,其中实例化多个 HTTP 客户端并且未正确释放连接会导致网络连接堆叠并且永远不会关闭.尝试httpPost.releaseConnection()
.您可能还对 HTTP 客户端文档感兴趣,第 1.1 章.5,确保释放低级资源"
- Is your server under heavy usage that might cause multiple network connections to be opened?
- The HTTP client documentation recommends to instantiate only one
HttpClient
and to reuse this instance. They are cases where instantiating multiple HTTP clients and not releasing connections correctly causes network connections to stack and never be closed. Try tohttpPost.releaseConnection()
. You might also be interested in the HTTP client documentation, chapter 1.1.5, "Ensuring release of low level resources"
这篇关于java.net.SocketException:没有可用的缓冲区空间(达到最大连接数?):连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java.net.SocketException:没有可用的缓冲区空间(达到最大连接数?):连接
基础教程推荐
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01