2015-06-30 35 views
1

我寫了一個Java代碼如下失敗的地方例外:java.net.SocketException異常:沒有可用的緩衝空間(最大連接數達到?):

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

這裏我發送一個GET請求到Web接受ip作爲參數併發送國家及其區域作爲響應的服務。

這工作好幾天,但後來它開始拋出「本地異常失敗:java.net.SocketException:沒有可用的緩衝區空間(最大連接到達?)」。 我搜索谷歌和它的大部分指定斷開http對象。我也做了,但我得到同樣的錯誤。任何一個可以幫助我在這個

回答

0

這可能是非常好的服務器端問題esp。由於端口號限制,它是Windows Server 2003上非常流行的一種。你的服務器端細節是什麼?

參考對方的回答,no buffer space available

0

在你的代碼來看,可能的解釋是,你的代碼斷開故障。您在try { ... }區塊內呼叫rd.close()conn.disconnect()。如果拋出異常,則catch將處理該錯誤...但塊的末尾將不會發生conn.disconnect()調用。根據連接是否爲「持久性」,這可能導致連接泄漏。

對此進行編碼的正確方法是確保清理代碼位於finally塊中。

相關問題