Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.SocketException: No buffer space available (maximum connections reached?): connect

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;
    }
like image 911
user381878 Avatar asked May 20 '11 06:05

user381878


People also ask

What is no buffer space available error?

The "no buffer space available" error indicates a lack of buffer space available for the TCP/IP stack. When this happens, any programs(discovery/polling) that attempt to create additional resources, like ping or snmp, will fail with this type of error.


2 Answers

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.

What to look for:

  • 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 to httpPost.releaseConnection(). You might also be interested in the HTTP client documentation, chapter 1.1.5, "Ensuring release of low level resources"
like image 191
Vivien Barousse Avatar answered Sep 23 '22 20:09

Vivien Barousse


The server has to few "Ephemeral port" defined se following to links : http://dbaktiar-on-java.blogspot.ro/2010/03/hudson-shows-buffer-space-available.html http://support.microsoft.com/kb/196271

This is solved : followed the steps above an "out of sockets" errors are gone.

Issue is limited to 2003 server.

like image 43
Nicu Avatar answered Sep 24 '22 20:09

Nicu