简体   繁体   中英

java.net.SocketException: Permission denied: connect. What could be the cause and how to avoid it?

I'm running into a java.net.SocketException (Permission denied: connect) when sending a lot of requests to a server. I've tried the -Djava.net.preferIPv4Stack=true option as mentioned in other threads. This issue only occurs after a lot of connections have already been made. The following code can be used to reproduce the issue:

public class Example {
  public static void main(String[] args) {
    if(args.length == 1) {
      System.out.println(args[0]);
      for(int i = 0; i < Integer.MAX_VALUE; i++) {
        requestURL(args[0]);
      }
    }
  }
  public static void requestURL(String targetUrl) {
    URL url = new URL(targetUrl);
    HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
    httpCon.setDoInput(true);
    BufferedReader rd = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
    //handle response here
    rd.close();
    httpCon.disconnect();
  }
}

The code will successfully send the requests until the Exception occurs. This has been verified in the server's log files.

Please run the sample code only against a server that you are permitted to, as it generates quite a bit of traffic / load on the server.

So the question now is: How to avoid that Exception? Or any workarounds? And what is the actually issue here?

EDIT

added httpCon.disconnect() . (doesn't solve the problem)

EDIT #2 (StackTrace)

java.net.SocketException: Permission denied: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.protocol.http.HttpURLConnection.connect(Unknown Source)
    at Example.requestUrl(Example.java)
    at Example.main(Example.java)

When the issue occurs no further requests are sent to the server (no log entries on the server and no SYN packets (sniffed with tcpdump)).

EDIT #3

I've forgotten to run the application with the -Djava.net.preferIPv4Stack=true argument. It hasn't thrown an exception yet when using both, the aforementioned argument and the httpCon.disconnect() method.

尝试使用 httpCon.disconnect() 确保在完成读取响应后重新释放套接字。

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