title: java.rmi.ConnectIOException & java.net.SocketException: Socket operation on nonsocket: connect
body: <p>I'm an italian guy, so sorry for my BAD english.</p>
  
  <p>I'm writing a RMI program in Java, and this is the section of the node's code to connect to the RMI server:</p>
  
  <pre><code>try
          {//trying to connect to the Server
          Remote RemoteObject;
          BootstrapServer_Interface serverObject;
          Registry r = LocateRegistry.getRegistry(10001);
          RemoteObject = r.lookup("BOOTSTRAP-SERVER");//THIS IS LINE 52
          serverObject = (BootstrapServer_Interface)RemoteObject;
          //I get an ip as Server's answerw
          System.out.println("Il nodo "+Thread.currentThread()+" si mette in attesa");
          String ipBN = serverObject.start_join_node(fakeip);
          if(ipBN.equals("-1"))
              {System.out.println("Creazione del nodo "+Thread.currentThread()+" fallito");
              return;
              }
  </code></pre>
  
  <p>However, <strong>sometimes</strong>, the program get this exception:</p>
  
  <pre><code>java.rmi.ConnectIOException: Exception creating connection to: 131.114.43.246; nested exception is: 
      java.net.SocketException: Socket operation on nonsocket: connect
      at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
      at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
      at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
      at sun.rmi.server.UnicastRef.newCall(Unknown Source)
      at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
      at StorageNode.run(StorageNode.java:52)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  Caused by: java.net.SocketException: Socket operation on nonsocket: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(Unknown Source)
      at java.net.PlainSocketImpl.connectToAddress(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 java.net.Socket.&lt;init&gt;(Unknown Source)
      at java.net.Socket.&lt;init&gt;(Unknown Source)
      at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
      at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
      ... 9 more
  </code></pre>
  
  <p>And this the client's code to connect to the RMI server:</p>
  
  <pre><code>try
              {//scelgo casualmente un dato da cercare        
              //genero il mio indirizzo ip
              fakeip = String.valueOf((int) (Math.random() * 256)) + "."
                      + String.valueOf((int) (Math.random() * 256)) + "."
                      + String.valueOf((int) (Math.random() * 256)) + "."
                      + String.valueOf((int) (Math.random() * 256));
              //creo una Socket UDP con cui contattare il Nodo di Bootstrap
              UDPServiceSocket = scanner.UDPSocket();
              if(UDPServiceSocket==null)
                  {System.out.println("Tentativo di creazione socket UDP fallito...");
                  *return;}
              int UDPport = UDPServiceSocket.getLocalPort();
              fakeip=fakeip+" UDPport="+UDPport;
              //mi connetto al server tramite RMI
              Remote RemoteObject;
              Registry r = LocateRegistry.getRegistry(10001);
              RemoteObject = r.lookup("BOOTSTRAP-SERVER");//THIS IS LINE 48
              serverObject = (BootstrapServer_Interface)RemoteObject;
              //Ottengo l'ip del Bootstrap Node a cui connettermi
              System.out.println("Il client "+Thread.currentThread()+" si mette in attesa.");         
              ipBN = serverObject.client_search();
              }
  </code></pre>
  
  <p>And the exception in this case <strong>(that not necessarily happen together with the node exception that I Posted above)</strong></p>
  
  <pre><code>java.rmi.ConnectIOException: Exception creating connection to: 131.114.43.246; nested exception is: 
      java.net.SocketException: Socket operation on nonsocket: connect
      at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
      at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
      at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
      at sun.rmi.server.UnicastRef.newCall(Unknown Source)
      at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
      at Client.run(Client.java:48)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  Caused by: java.net.SocketException: Socket operation on nonsocket: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(Unknown Source)
      at java.net.PlainSocketImpl.connectToAddress(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 java.net.Socket.&lt;init&gt;(Unknown Source)
      at java.net.Socket.&lt;init&gt;(Unknown Source)
      at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
      at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
  </code></pre>
  
  <p>I can't find any reason that cause this exception. </p>
  
  <p>I'm sorry for the less code that I posted before, but it's a little part of a big project, so I cannot past all the code :D</p>
  
  <p>However, if you need it, I can give you some informations about it:</p>
  
  <ol>
  <li><p>This is a DHT project, and this is the node code while he's trying
      to contact the Bootstrap Server (indeed, a RMI Server). </p></li>
  <li><p>The RMI server methods could be called by many nodes (the code above) and/or
      many client at the same time. </p></li>
  <li>The server methods called by nodes
      and/or clients are created to manage a Monitor installed on the
      Server. </li>
  <li>Through this monitor, the nodes and clients modifies an Hash
      Table following the model of "writers and readers" (mutual exclusion
      ecc.).</li>
  </ol>
  
  <p>Thanks a lot to everyone that will help me with this issue, and sorry again for my bad english!</p>