Unknown host RMI java

172 Views Asked by At

I get an unknown host error when I try to start an RMI server here. The error is unknown host. Actually, I'm just a beginner in RMI and sockets programming in JAVA. Can you please provide me with some tutorials concerning my problem.

My interface code:

package ApplicationServer;    
import java.io.File;
import java.rmi.RemoteException;
import java.sql.ResultSet;

public interface Méthodes {

    public String authentification(String login, String mdp) throws RemoteException;
    public ResultSet selection(String requete) throws RemoteException;
    public int miseAjour(String requete) throws RemoteException;
    public String envoyerFichier(File fichier) throws RemoteException;
}

In this class I have all the methods that I need in my program:

package ApplicationServer;
import java.io.File;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Traitements extends UnicastRemoteObject implements Méthodes {

    private Connection cnx;
    private Statement stm;
    private ResultSet rst;
    private int rs;

    public void setCnx(Connection cnx) {
        this.cnx = cnx;
    }

    public void setStm(Statement stm) {
        this.stm = stm;
    }

    public void setRst(ResultSet rst) {
        this.rst = rst;
    }

    public void setRs(int rs) {
        this.rs = rs;
    }

    protected Traitements() throws RemoteException {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public String authentification(String login, String mdp) throws RemoteException {
        // TODO Auto-generated method stub
        try {

            setCnx(null);
            setStm(null);
            setRst(null);

            Class.forName("com.mysql.jdbc.Driver"); 
            cnx=DriverManager.getConnection("jdbc:mysql://localhost:3306/banque","root","");
            stm=cnx.createStatement();

            rst=stm.executeQuery("select md5('"+mdp+"');");
            while(rst.next()){
                mdp=rst.getString(1);
            }

            stm.clearBatch();
            stm=cnx.createStatement();
            setRst(null);
            String Query = "select * from utilisateurs where login like '"+login+"' and motdepasse like '"+mdp+"';"; 
            rst=stm.executeQuery(Query);

            int id = 0;
            String nm = null;
            String prm = null;

            while(rst.next()){
                id = rst.getInt("id");
                nm = rst.getString("nom");
                prm = rst.getString("prenom");  
            }
            if(id>0 && nm!=null && prm!=null){
                return id+" "+nm+" "+prm;   
            }
        }catch (SQLException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        return null;
    }

    @Override
    public ResultSet selection(String requete) throws RemoteException {
        try{
            setCnx(null);
            setStm(null);
            setRst(null);

            Class.forName("com.mysql.jdbc.Driver"); 
            cnx=DriverManager.getConnection("jdbc:mysql://localhost:3306/banque","root","");
            stm=cnx.createStatement(); 
            rst=stm.executeQuery(requete);

        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rst;
    }

    @Override
    public int miseAjour(String requete) throws RemoteException {
        try{
            setCnx(null);
            setStm(null);
            setRs(0);

            Class.forName("com.mysql.jdbc.Driver"); 
            cnx=DriverManager.getConnection("jdbc:mysql://localhost:3306/banque","root","");
            stm=cnx.createStatement(); 
            rs=stm.executeUpdate(requete);

        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return rs;
    }
    @Override
    public String envoyerFichier(File fichier) throws RemoteException {
        // TODO Auto-generated method stub
        return null;
    }

}

This is the main class that runs the server:

package ApplicationServer;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import javax.swing.JOptionPane;

public class Main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            int port=12345;
            String url="rmi://serveur:"+port+"/reference";      
            LocateRegistry.createRegistry(port);
            Traitements srv = new Traitements();
            Naming.rebind(url, srv);

            JOptionPane.showMessageDialog(null, "Serveur Lance :");

        } catch (RemoteException | MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } 
}
1

There are 1 best solutions below

0
On

You are supposed to replace serveur with a real hostname in that code.

However it's far better to use localhost in the RMI URL when binding. The Registry has to be local to the host anyway so there's no point in using anything else.