title: java.net.SocketException: Permission denied: connect. What could be the cause and how to avoid it?
body: <p>I'm running into a <code>java.net.SocketException</code> (Permission denied: connect) when sending a lot of requests to a server. I've tried the <code>-Djava.net.preferIPv4Stack=true</code> 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:</p>
  
  <pre><code>public class Example {
    public static void main(String[] args) {
      if(args.length == 1) {
        System.out.println(args[0]);
        for(int i = 0; i &lt; 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();
    }
  }
  </code></pre>
  
  <p>The code will successfully send the requests until the Exception occurs. This has been verified in the server's log files.</p>
  
  <p>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.</p>
  
  <p>So the question now is: How to avoid that Exception? Or any workarounds? And what is the actually issue here?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>added <code>httpCon.disconnect()</code>. (doesn't solve the problem)</p>
  
  <p><strong>EDIT #2 (StackTrace)</strong></p>
  
  <pre><code>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.&lt;init&gt;(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)
  </code></pre>
  
  <p>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)).</p>
  
  <p><strong>EDIT #3</strong></p>
  
  <p>I've forgotten to run the application with the <code>-Djava.net.preferIPv4Stack=true</code> argument.
  It hasn't thrown an exception yet when using both, the aforementioned argument and the <code>httpCon.disconnect()</code> method.</p>