Problème avec client RMI

newgame Messages postés 52 Date d'inscription lundi 2 février 2009 Statut Membre Dernière intervention 24 juillet 2013 - 5 déc. 2012 à 19:07
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 6 déc. 2012 à 08:29
salut,
j'ai TP RMI à rendre et voilà que je me trouve bloqué avec une erreur que je n'arrive pas à résoudre depuis 2 jours
java.lang.ClassCastException: $Proxy0 cannot be cast to rmi.Rmiinterface
at rmi.rmiclient$1.actionPerformed(rmiclient.java:37)
en plus je n'arrive pas à récupéré les valeurs que j'ecris dans les champs texte du clientrmi
voici mon code:

***************rmiclient*******************
package rmi;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.*;

public class rmiclient extends javax.swing.JFrame {
JTextField prenom = new JTextField();
JTextField nom = new JTextField();
JLabel p = new JLabel("prenom");
JLabel n = new JLabel("nom");
JButton b =new JButton("print");
public rmiclient() {

GridLayout g = new GridLayout(3, 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(g);
getContentPane().add(p);
getContentPane().add(prenom);
getContentPane().add(n);
getContentPane().add(nom);
getContentPane().add(b);
b.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent arg0) {
//JOptionPane.showMessageDialog(null,"nom: "+ nom.getText()+ " prenom: "+prenom.getText());
try {
Registry registry=LocateRegistry.getRegistry("127.0.0.1",(new Integer("3333").intValue()));
Rmiinterface rmiServer = (Rmiinterface)registry.lookup("rmiServer");
Scritique.lock();
String pr=prenom.getText();
String no=nom.getText();
rmiServer.setNom(no);
Thread.sleep(5000);
rmiServer.setPrenom(pr);
rmiServer.print();
Scritique.unlock();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
});


this.setVisible(true);
pack();

}


public static void main(String args[])
{
rmiclient c = new rmiclient();

}



}


*******************serverrmi************************
package rmi;
import java.rmi.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;


public class serverrmi extends java.rmi.server.UnicastRemoteObject
implements Rmiinterface
{

int thisPort;

String thisAddress;

Registry registry; // rmi registry for lookup the remote objects.
public String nom;
public String prenom;



// This method is called from the remote client by the RMI.

// This is the implementation of the “ReceiveMessageInterface”.
public void setPrenom(String p) throws RemoteException {
this.prenom=p;
}



public void setNom(String n) throws RemoteException {
this.nom=n;

}



public void print() throws RemoteException {
System.out.println("Nom: "+nom);
System.out.println("Prenom: "+prenom);

}



public serverrmi() throws RemoteException

{

try{

// get the address of this host.

thisAddress="127.0.0.1";

}

catch(Exception e){

throw new RemoteException("can't get inet address.");

}

thisPort=3333; // this port(registry’s port)

System.out.println("this address="+thisAddress+",port="+thisPort);

try{

// create the registry and bind the name and object.

registry = LocateRegistry.createRegistry( thisPort );

registry.rebind("rmiServer", this);

}

catch(RemoteException e){

throw e;

}

}



static public void main(String args[])

{

try{

serverrmi s=new serverrmi();
System.out.println("Serveur en marche");

}

catch (Exception e) {

e.printStackTrace();

System.exit(1);

}

}




}

**************************Rmiinterface*********
package rmi;
import java.rmi.*;

public interface Rmiinterface {
void setPrenom(String p) throws RemoteException;
void setNom(String n) throws RemoteException;
void print() throws RemoteException;

}
j'éspére recevoir vos réponse aussi vite que possible car je dois rendre mon travail cette semaine
merci d'avance

1 réponse

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
6 déc. 2012 à 08:29
Salut,

Le problème vient de ce cast :
Rmiinterface rmiServer = (Rmiinterface)registry.lookup("rmiServer");

Mais a vrai dire, je ne comprend pas trop ce que tu essayes d'obtenir...
0
Rejoignez-nous