java.net.SocketException:没有可用的缓冲区空间(达到最大连接数?):connect

时间:2011-05-20 06:49:57

标签: java sockets connection httpclient socketexception

您好我正在使用Apache HTTP Client 4.0在服务器上上传基于HTTPS协议的文件。上传的应用程序全天候运行。今天突然间它开始抛出这个异常 -

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)

任何人都可以帮助我吗?我完全不知道发生了什么事?

这是上传文件的源代码 -

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;
    }

5 个答案:

答案 0 :(得分:21)

我的猜测:您的端口不足,问题与您的代码没有直接关系,而是与服务器的当前状态有关。对其他计算机打开的连接太多,这会导致问题。

寻找什么:

  • 您的服务器是否处于大量使用状态,可能会导致多个网络连接被打开?
  • HTTP client documentation建议仅实例化一个HttpClient并重新使用此实例。它们是cases,其中实例化多个HTTP客户端而不是正确释放连接会导致网络连接堆叠并且永远不会关闭。尝试httpPost.releaseConnection()。您可能还对HTTP客户端文档chapter 1.1.5, "Ensuring release of low level resources"
  • 感兴趣

答案 1 :(得分:2)

服务器必须少有短暂的端口&#34;定义链接后面的链接: http://dbaktiar-on-java.blogspot.ro/2010/03/hudson-shows-buffer-space-available.html http://support.microsoft.com/kb/196271

这已经解决了:按照上面的步骤&#34;出了套接字&#34;错误消失了。

问题仅限于2003服务器。

答案 2 :(得分:1)

可能因为您的服务器(数据库/ Http)连接耗尽而发生。使用连接池或减少最大连接可以解决此问题。

答案 3 :(得分:1)

这似乎是一个Windows问题,无论是关于临时端口,还是关于afd.sys中的错误,具体取决于您的Windows版本。 请参阅to my answer to a similar question on stackoverflow

答案 4 :(得分:0)

我的 windows2008 服务器几天前发生了同样的问题,我无法通过编码快速解决这个问题,然后我重新启动了我的服务器问题消失了,这个 er