5 Replies Latest reply: May 25, 2010 6:34 PM by EJP RSS

    Why does sun.nio.ch.Net.connect throw BindException?

    807580
      I found an older post about this here, but no resolution was reached on that thread:
      [http://forums.sun.com/thread.jspa?threadID=313168|http://forums.sun.com/thread.jspa?threadID=313168]

      The code I am running is:
      socketChannel = SocketChannel.open();
      socketChannel.configureBlocking(true);
      socketChannel.connect(
          new InetSocketAddress(connectRequest.getAddress(), 
                                              connectRequest.getPort())); 
      // i.e. The address of the server I am attempting to connect to.
      This works fine, but I have started stress testing recently, and am now opening lots of connections at once in say 10 or more threads. Now I sometimes get BindExceptions:
      testSession(com.rapidaddition.genzero.stress.SessionStressTest)
      java.lang.RuntimeException: java.net.BindException: Cannot assign
      requested address
      ...
      Caused by: java.net.BindException: Cannot assign requested address
              at sun.nio.ch.Net.connect(Native Method)
              at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:507)
      What is causing this? Is it a bug with the SocketChannelImpl? Could it be that I am not closing my connections properly, and consequently running out of local ports or file handles (I don't think I am failing to close connections)?

      I think I will place the connect code in a synchronized block, so that only one connection at once is made, and see if that prevents the problem. In practice that will not be a problem for me, as clients typically do not create lots of connections at once, but it may limit the effectiveness of my stress test somewhat.

      Thanks in advance for any advice you are able to give me.

      Rupert