简体   繁体   中英

Failed on local exception: java.net.SocketException: No buffer space available (maximum connections reached?):

I have written a java code as below

                String fulldetails ="";
                String line="";
                String result="";
                URL url;
                HttpURLConnection conn;
                BufferedReader rd;
                String country="";
                String region="";
                String city="";
                String zipcode="";
        try
            {

                   String ip = "xxx.xxx.xx.x";
                    url = new URL("http://xxx.xx.xxx.x:2298/api/sample?ip="+ip);

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

                    rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                            while ((line = rd.readLine()) != null) 
                            {
                                result += line;
                            }

                    System.out.println(result);
                    rd.close();
                    result = result.substring(1, result.length()-1);

                    JSONObject json_object = new JSONObject(result);
                    country = (String) json_object.get("Country");
                    System.out.println(country);
                    region = (String) json_object.get("Region");
                    System.out.println(region);
                    city = (String) json_object.get("City");
                    System.out.println(city);
                    zipcode = (String) json_object.get("ZipCode");
                    System.out.println(zipcode);
                    fulldetails = "Country:"+country+",Region:"+region+",City:"+city+","+"ZipCode:"+zipcode;
                    System.out.println(fulldetails);

                    conn.disconnect();
            }

            catch(Exception exception)
                {
                    exception.printStackTrace();;
                 }

Here I am sending a get request to a web services which accepts ip as parameter and sends country and its region as its response.

This works fine for few days but later it begin to throw "Failed on local exception: java.net.SocketException: No buffer space available (maximum connections reached?)". I have searched google and most of it specifies to disconnect the http object. I had also done it but I ma getting the same error. Can any one help me in thsi

This might very well be server side issue esp. the very popular one on Windows Server 2003 due to port number limits. What are your server side details?

Refer other answer , no buffer space available

Looking at your code, the likely explanation is that your code for disconnecting is faulty. You are calling rd.close() and conn.disconnect() within the try { ... } block. If an exception is thrown, the catch will handle it ... but the conn.disconnect() call at the end of the block won't happen. Depending on whether the connection is "persistent" or not, this could result in the connection leaking.

The correct way to code this is to make sure that the cleanup code is in a finally block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM