简体   繁体   中英

RMI Server Connection refused to host: ############ ; nested exception is: java.net.ConnectException: Connection refused: connect

I'm making my first RMI server (also including it in a project I'm working on). This is my error:

Exception in thread "main" java.rmi.ConnectException: Connection refused to host: 192.168.11.1;
nested exception is:
java.net.ConnectException: Connection refused: connect

This is the main:

import java.rmi.Remote;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;


public class mainServidor {

    public static void main(String[] args) throws Exception{
          utils.setCodeBase(IServidor.class);
          Servidor_Cubo servidor = new Servidor_Cubo();
          IServidor remote = (IServidor)UnicastRemoteObject.exportObject(servidor, 8888);

          Registry registry = LocateRegistry.getRegistry();
          registry.rebind("retoAleatorio", remote);
          System.out.println("servidor corriendo, presione enter para terminar");
          System.in.read();
          registry.unbind("retoAleatorio");
          UnicastRemoteObject.unexportObject(servidor, true);
    }
}

this is the Server class dont pay attention to all the FILEREADING stuff

import java.io.File;
import java.io.FileNotFoundException;
import static java.lang.Math.abs;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

/**
 *
 * @author Emmanuel
 */
public class Servidor_Cubo implements IServidor {
     ArrayList<Reto> ArrayRetos;
     Map<Integer,String> mapaJugadores_Integer = new HashMap<Integer,String>();
     Map<String,Integer> mapaJugadores_String = new HashMap<String,Integer>();

    public static int sesion = abs(new Random().nextInt());
    @Override
    public int AgregarUsuarioNuevo(String nombre) throws RemoteException {
        int id = sesion++;
              mapaJugadores_Integer.put(id, nombre);
              mapaJugadores_String.put(nombre, id);
        return id;   
    }


    @Override
     public Integer autenticar(String nombre){
            return mapaJugadores_String.get(nombre);
     }

     @Override
     public String TraerRetoAleatorio(int num){

             return leerArchivoMovimientos(num);


     }


     public void agregarGanador(String Nombre, int Mov,int num){
         int id = mapaJugadores_String.get(Nombre);

     }

     public void cargarServidor( ){
    File fReto = new File ("D:/repos/RubikFX-master/Retos/ListadoDeRetos.txt"); //Ingresa ruta del .txt de retos
    File fJugadores = new File("D:/repos/RubikFX-master/Retos/Jugadores.txt"); //Ingresa ruta del .txt de jugadores


    Scanner scanReto;
    Scanner scanJugador;
        try {

                System.out.println("SI FUNCIONAAA!!!");
                scanReto = new Scanner(fReto);
                scanJugador = new Scanner(fJugadores);
                ArrayList<Reto> retos = new ArrayList<>();
            while(scanReto.hasNext()){
                Reto reto = new Reto();
                ArrayList<Jugador> lista = new ArrayList<>();
                reto.setReto(scanReto.nextLine());

                String[] datoReto = scanJugador.nextLine().split(";");

                for(int i = 0; i < datoReto.length; i++ ){
                    Jugador jug = new Jugador();
                    String str = datoReto[i];
                    String[] datosJugador = str.split(",");
                    jug.setNombre(datosJugador[0]);
                    //int foo = Integer.parseInt("1234");
                    jug.setId (Integer.parseInt(datosJugador[1]));
                    jug.setMovimientos(Integer.parseInt(datosJugador[2]));
                    lista.add(jug);
                }

                reto.setGanadores(lista);
                retos.add(reto);
             }

        } catch (FileNotFoundException ex) {

            System.out.println("NO FUNCIONA!!!");

        }

    } 

this is the interface IServidor

package servidor_cubo;
import java.rmi.Remote;
import java.rmi.RemoteException;


public interface IServidor extends Remote {

    public Integer autenticar(String nombre) throws RemoteException;

        public int AgregarUsuarioNuevo(String nombre) throws RemoteException;

        public String TraerRetoAleatorio(int num) throws RemoteException;

}

I use this utils class to set the classpath (saw in a tutorial)

public class utils {
    public static final String CODEBASE = "java.rmi.server.codebase";


    public static void setCodeBase(Class<?> c) {
        String ruta = c.getProtectionDomain().getCodeSource()
                       .getLocation().toString();

        String path = System.getProperty(CODEBASE);

        if (path != null && !path.isEmpty()) {
            ruta = path + " " + ruta;  
        }

        System.setProperty(CODEBASE, ruta);
    } 
}

i already "javaw rmiregistry" in the cmd.... so... i dont know what to do. please, help me out.

Using win8.1...

At the very end of your question you said you are running Windows 8.1. Starting with Windows 7, the firewall is enabled by default. My money is on the firewall blocking traffic to your RMI service.

You have a couple options:

  • Use the loopback address (127.0.0.1 for IPv4 and ::1 for IPv6)
  • Unblock the application in the firewall
  • Unblock the port in the firewall
  • Turn off the firewall completely (not recommended)

The Windows Firewall prefers you to open ports on an application by application basis. It's a lot easier to do that than it is to open the port yourself. I recommend managing it this way as it's a lot easier to work with. If it's a one-time set up, then you can unblock the application by going through your control panel, in the "System and Security Section" you should see the "Firewall" options. One of the options is "Allow a program through Windows firewall". You should also be able to find it by searching for the window firewall and finding the same option on the left hand commands.

If you want to automate the process, for example in an install script, you can manage the firewall through the command line . For quick reference:

netsh advfirewall firewall add rule name="Secure App" dir=in action=allow program="C:\Program Files\SecureApp.exe" enable=yes

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