Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java RMI Error

  • 31-03-2012 10:24PM
    #1
    Closed Accounts Posts: 2,696 ✭✭✭


    Hey peoples - my java rmi knowledge is fairly basic- i have setup server and client for the obligitory java chat program

    I keep gettin error below and cannot for the life of me get around it (have been staring at screen for too long) - eclipse is happy with all my code

    can anyone tell me where im going wrong?


    Error
    [U][SIZE=2][COLOR=#000080][SIZE=2][COLOR=#000080]java.lang.ClassCastException[/COLOR][/SIZE][/COLOR][/SIZE][/U][SIZE=2][COLOR=#ff0000][SIZE=2][COLOR=#ff0000]: $Proxy0 cannot be cast to ChatServer[/COLOR][/SIZE]
    [/COLOR][/SIZE]
    


    Server Code
     
    import java.rmi.Naming;
    import java.rmi.RemoteException;
    import java.rmi.server.UnicastRemoteObject;
    import java.util.LinkedList;
     
     
    public class DITChatServer extends UnicastRemoteObject {
     
     /**
      * 
      */
     private static final long serialVersionUID = 1L;
     
     // linked list
     public LinkedList<ChatClient> logins;
     // constructor - creates 
     public DITChatServer() throws RemoteException {
      logins = new LinkedList<ChatClient>();
     
     
     }
     
     //method to record new client (add client to linked list)
     public void newClient(ChatClient name) throws RemoteException {
      logins.add(name);
     }
     
     //method to send message (uses receive method for clients)
     public void sendMessage(String text) throws RemoteException {
      for (int i = 0; i < logins.size(); i++) {
       logins.get(i).getMessage(text);
      }
     }
     
     // in main bind service
     public static void main(String[] args) {
      try {
       Naming.rebind("rmi://localhost/chat", new DITChatServer());
      } catch (Exception e) {
       System.err.println(e);
       System.err.println("Error Binding");
      }
     
      System.err.println("Accepting Connections");
     
     }
     
    }
     
    

    Client Code
     
    import java.rmi.Naming;
    import java.rmi.RemoteException;
    import java.util.Scanner;
     
    public class DITChatClient implements ChatClient, Runnable{
     
     //create instance of chat server and call server
     public ChatServer server;
     
     
     //constructor for DIT Chat Client
     public DITChatClient(ChatServer cs) throws RemoteException {
     
      server = cs;
      server.newClient(this);
     
     }
     
     public void getMessage(String message) throws RemoteException{
      System.out.println("Message :" + message);
     }
     
     public synchronized void run() {
      Scanner in = new Scanner(System.in);
      String text;
      while (true) {
       try {
        text = in.nextLine();
        server.sendMessage(text);
       } catch (Exception e) {
        System.err.println(e);
       }
      }
     }
     
     public static void main(String[] args) {
     
      try {
       ChatServer cs = (ChatServer) Naming.lookup("rmi://localhost/chat");
       new Thread(new DITChatClient(cs)).start();
      } catch (Exception e) {
       System.err.println(e);
      }
     }
     
     
     
     
     }
     
    

    Server Interface
     
    import java.rmi.Remote;
    import java.rmi.RemoteException;
     
    public interface ChatServer extends Remote{
     void newClient(ChatClient name) throws RemoteException;
     void sendMessage(String text) throws RemoteException;
     }
     
    

    Client Interface
     
     
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#7f0055]
    [LEFT][/COLOR][/SIZE][SIZE=2]java.rmi.*;[/SIZE][/LEFT]
     
    
    [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][/LEFT]
    [SIZE=2][COLOR=#7f0055]
     
    [LEFT][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]interface[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ChatClient [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] [U]Remote[/U]{[/SIZE]
    
    [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][/LEFT]
    [SIZE=2][COLOR=#7f0055]
     
    [LEFT][/COLOR][/SIZE][SIZE=2]getMessage(String message) [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]throws[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] RemoteException;[/SIZE]
    [SIZE=2]}[/SIZE][/LEFT]
     
    


Comments

  • Closed Accounts Posts: 2,696 ✭✭✭mark renton


    Another day another dawn - i missed chat server implementation implementing chatserver interface

    also extending UnicastRemoteObject on client implementation


Advertisement