RMI + comment appeler la méthode distante
Bonjour ,
J'ai un projet à faire en RMI.
J'ai fait un client et un serveur, le client fait appel à une méthode donnerNom sur le serveur qui lui permet de récuperer un bonjour+nom.
Pour l'instant je veux juste réussir un appel .
Voici le code de l'implémentation
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public class pairImlp extends java.rmi.server.UnicastRemoteObject implements PairInt {
// On va redéfinir les méthodes
int n =0 ;
private String nom ;
// Le constructeur de cette classe
public pairImlp(String nom) throws java.rmi.RemoteException
{
this.nom = nom ;
}
public String donnerNom(String nom) throws java.rmi.RemoteException
{
String ch="Bonjour" ;
ch = ch+nom ;
return ch ;
}
} |
Voici le code du serveur
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
import java.rmi.*;
public class Serveur {
// Les attributs
// Le constructeur
public Serveur()
{
try {
// Création d'un objet de l'mplémentation
PairInt noeud1 = new pairImlp("noeud1") ;
System.out.println("L'objet a été créé") ;
// Inscription dans le rmiregistry
Naming.bind("noeud1",noeud1);
System.out.println("L'objet a été inscrit dans le registry") ;
System.out.println("Je suis en attente") ;
}
catch(Exception e){
}
}
// Classe de Test
public static void main(String []argv)
{
Serveur instance = new Serveur() ;
}
} |
ET enfin voici le code du client je crois que j'ai un problème dans la
partie en gras
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
public class Client {
// Les attributs
String nom ;
String n ;
// Le constructeur
public Client(String nom){
try{
this.nom = nom ;
// le client implemente aussi l'interface
PairInt noeudClient = new pairImlp(nom) ;
System.out.println("Un objet client a été créé ") ;
// Le client va aussi s'inscrire dans le rmiregistry
// par un Naming.bind
Naming.bind(nom,noeudClient) ;
System.out.println("Je suis bien enregistré dans le registry") ;
// En même temps il va rechercher un autre client
PairInt ref = (PairInt)Naming.lookup("rmi/localhost/noeud1") ;
n = ref.donnerNom(nom);
System.out.println(n) ;
}
catch (Exception e){
System.out.println(e.getMessage()) ;
}
} // Fin du constructeur
// Début de la classe de test
public static void main (String [] argv)
{
Client client = new Client("joseph") ;
}
} |
Actuellement mon objet client est crée,il s'inscrit dans le registry mais il ne m'affiche pas bonjour + le nom.
Merci d'avance pour votre aide