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

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

      I found an older post about this here, but no resolution was reached on that thread:

      The code I am running is:
      socketChannel = SocketChannel.open();
          new InetSocketAddress(connectRequest.getAddress(), 
      // 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:
      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.