Results 1 to 1 of 1
- 01-28-2011, 01:09 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 1
- Rep Power
- 0
RMI Client/Server Naming.lookup "$Proxy1 cannot be cast"
Hi i'm new and i'm just starting to use RMI. I'm writing a simple example of a client/server application (just a simple stupid game). So my client can't registry to rmiregistry. I post here my client (Giocatore.java) and my server (Mazziere.java)
The line about the problem are marked in red
Giocatore.java (Client-side)
Mazziere.java (Server-side)Java Code:[SIZE="2"]import java.rmi.*; import java.rmi.server.*; import java.util.logging.Logger; import java.io.*; public class Giocatore extends UnicastRemoteObject implements IClient { protected Giocatore() throws RemoteException { super(); } static Logger logger = Logger.getLogger("global"); private static final long serialVersionUID = 1L; public void dici(String message) throws RemoteException { System.out.println(message); } public int getCifra() throws RemoteException { return punti; } public String getNickname() throws RemoteException { return nickname; } public void removePunti(int p) throws RemoteException { punti -= p; } public void addPunti(int p) throws RemoteException{ punti += p; } public void esci() { System.out.println("Stai uscendo da Dummy!"); System.exit(0); } public static void main(String args[]){ String cmd; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // istanziare l'oggetto try{ giocatore = new Giocatore(); }catch (RemoteException e){ logger.severe("Problemi con la creazione dell'oggetto giocatore: " + e.getMessage()); e.printStackTrace(); } // controllare i parametri if(args.length < 2){ System.out.println("[Esecuzione] parametri da passare: host nickname"); System.exit(0); }else{ giocatore.host = "localhost"; giocatore.nickname = "Remigio"; } // effettuare lookup // TODO Oltre a non vedere nel debugger l'istanza Giocatore il problema piu grave è che non riesce a iscriversi a Dummy [B][COLOR="Red"]try{ mazziere = (Mazziere) Naming.lookup("rmi://"+giocatore.host+"/Dummy"); logger.info("Registrato al servizio di naming ora mi iscrivo"); boolean exit = mazziere.iscriviNuovo(giocatore); if (exit){ System.out.println("Server: Siamo al completo mi dispiace"); System.exit(0); } }catch(Exception e){ logger.severe("Errore nella registrzione sul server: " + e.getMessage()); e.printStackTrace(); System.exit(1); }[/COLOR][/B] // parte logica while(giocatore.punti>0){ try{ while (!(cmd = ask(in)).equals("!quit")){ if(cmd.startsWith("!")){ // sono comandi int num = Integer.parseInt(cmd.substring(1)); Puntata p = new Puntata(giocatore, num); mazziere.punta(p); giocatore.removePunti(p.getPuntata()); } else{ // si tratta della chat mazziere.dici(cmd, giocatore); } } }catch(Exception e){ e.printStackTrace(); } } System.exit(0); } private static String ask(BufferedReader in) throws IOException{ System.out.println("[" + giocatore.nickname + "@Dummy]: "); return(in.readLine()); } private String host; private int punti = 40; private String nickname; private static Giocatore giocatore; public static Mazziere mazziere; }[/SIZE]
The error reported isJava Code:[SIZE="2"] import java.rmi.*; import java.rmi.server.*; import java.io.*; import java.util.logging.Logger; import java.util.*; public class Mazziere extends UnicastRemoteObject implements IMazziere { private static final long serialVersionUID = 1L; static Logger logger = Logger.getLogger("global"); public Mazziere()throws RemoteException{ super(); } public void dici(String message, Giocatore rifCli) throws RemoteException { String s = "[" + rifCli.getNickname() + "] dice: " + message; Giocatore tmp; for(int i=0; i<giocatori.size(); i++){ tmp = giocatori.get(i); System.out.println(s); tmp.dici(s); } } public void punta(Puntata p) throws RemoteException { puntate.add(p); } public static void main(String args[]) throws RemoteException{ //setta il Security Manager e crea l'oggetto String cmd; System.setSecurityManager(new RMISecurityManager()); [B][COLOR="red"]try{ mazziere = new Mazziere(); Naming.rebind("Dummy", mazziere); System.out.println("Pronto per accettare Dummers!"); }catch (Exception e){ logger.severe("Problemi con creazione/rebind: " + e.getMessage()); e.printStackTrace(); }[/COLOR][/B] //Crea lo strIN BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Sono in attesa che si completino le iscrizioni poi inizierà il gioco!"); try{ while(!(cmd = ask(in)).equals("!quit")){ if(cmd.startsWith("!")){//zona comandi if(giocatori.size() == 4){ // iscrizioni compeltate System.out.println("Sono completate le iscrizioni. Attendo comandi!"); if(cmd.equals("!round")){ // inizia il gioco for (Giocatore dummer: giocatori) { dummer.dici("Dummers: il gioco inizia, puntate!"); } while(true){// accetta scommesse e attende if(puntate.size()==4){ System.out.println("Scommesse completate decidere il criterio: [!low / !high]"); if((cmd = ask(in)).equals("!low")){ mazziere.vince("low"); break; } else if((cmd = ask(in)).equals("!high")){ mazziere.vince("high"); break; } else System.out.println(ERRORMSG); } } } } } }// end while(cmd) System.out.println("Esco!"); System.exit(0); }catch(Exception e){ e.printStackTrace(); } } public boolean iscriviNuovo(Giocatore ref)throws RemoteException{ if( giocatori.size() < 4){ giocatori.add(ref); System.out.println("Si è iscritto " + ref.getNickname()); return true; }else return false; } private static String ask(BufferedReader in)throws IOException{ System.out.println("Server@Dummy > "); return (in.readLine()); } private void vince(String criterio) throws RemoteException{ Giocatore candidato = null; if (criterio.equals("low")){ int min = 11; for (Puntata dummer : puntate) { if(dummer.getPuntata()<=min){ min = dummer.getPuntata(); candidato = dummer.getGiocatore(); } } candidato.addPunti(min); }else{ int max = 0; for (Puntata dummer: puntate){ if(dummer.getPuntata()>max){ max = dummer.getPuntata(); candidato = dummer.getGiocatore(); } } candidato.addPunti(max); } for(Giocatore cli: giocatori){ cli.dici("Ha vinto " + candidato.getNickname() + "!!!"); } } private final static String ERRORMSG ="Comando non adatto"; private static Mazziere mazziere; static ArrayList<Giocatore> giocatori = new ArrayList<Giocatore>(); static ArrayList<Puntata> puntate = new ArrayList<Puntata>(); } [/SIZE]
SEVERE: Errore nella registrazione sul server(Error about registry): $Proxy1 cannot be cast to Mazziere
java.lang.ClassCastException: $Proxy1 cannot be cast to Mazziere
at Giocatore.main(Giocatore.java:60)
:confused:
In the server-side i've not any kind of problem.. so i think it do rebind, but in the client-side at the first line of try it print me this error.
Could someone help me please!? :)Last edited by aladinsane; 01-28-2011 at 01:20 AM.
Similar Threads
-
Exception in thread "main" javax.naming.NameNotFoundException: jmx not bound
By Vartika in forum Advanced JavaReplies: 2Last Post: 08-07-2009, 10:21 PM -
Exception in thread "main" java.lang.NoClassDefFoundError: client
By fithous in forum New To JavaReplies: 1Last Post: 04-02-2009, 09:50 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
Cannot choose runtime server, "Windows -> Preferences -> Server" does not exist!!
By TennSeven in forum EclipseReplies: 0Last Post: 09-18-2008, 05:07 PM -
Exception in thread "main" java.lang.NoClassDefFoundError: client Help
By b000m in forum New To JavaReplies: 6Last Post: 08-18-2008, 06:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks