【问题标题】:Failed on local exception: java.net.SocketException: No buffer space available (maximum connections reached?):本地异常失败:java.net.SocketException:没有可用的缓冲区空间(达到最大连接数?):
【发布时间】:2015-06-30 10:55:58
【问题描述】:

我写了一个java代码如下

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

在这里,我向接受 ip 作为参数并发送国家及其地区作为响应的 Web 服务发送 get 请求。

这几天可以正常工作,但后来它开始抛出“本地异常失败:java.net.SocketException:没有可用的缓冲区空间(达到最大连接数?)”。 我搜索了谷歌,其中大部分都指定断开 http 对象。我也做过,但我得到了同样的错误。任何人都可以帮助我吗

【问题讨论】:

    标签: java sockets http get


    【解决方案1】:

    这很可能是服务器端问题,尤其是。由于端口号限制,它在 Windows Server 2003 上非常流行。您的服务器端详细信息是什么?

    参考其他答案,no buffer space available

    【讨论】:

      【解决方案2】:

      查看您的代码,可能的解释是您的断开连接代码有问题。您在 try { ... } 块内调用 rd.close()conn.disconnect()。如果抛出异常,catch 将处理它......但块末尾的conn.disconnect() 调用不会发生。根据连接是否“持久”,这可能会导致连接泄漏。

      对此进行编码的正确方法是确保清理代码位于finally 块中。

      【讨论】:

        猜你喜欢
        • 2011-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多