Jay_

Untitled

Nov 27th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. package RMIChat;
  2.  
  3. /*
  4.  * To change this template, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7. import java.rmi.*;
  8. import java.rmi.server.UnicastRemoteObject;
  9. import java.util.Scanner;
  10.  
  11. /**
  12.  *
  13.  * @author Jay
  14.  */
  15. public class ChatClient extends UnicastRemoteObject implements Runnable, ChatService {
  16.    
  17.     public ChatClient() throws RemoteException {
  18.         super();
  19.     }
  20.    
  21.    
  22.     private ChatRemoteServer mycs;
  23.    
  24.     public void ChatClientImp(ChatRemoteServer cs) throws RemoteException
  25.     {
  26.         mycs = cs;
  27.         mycs.regClient(this);
  28.     }
  29.    
  30.     public synchronized void getMessage(String s) throws RemoteException {
  31.         System.out.println("Message: " + s);
  32.     }
  33.    
  34.     public void run()
  35.     {
  36.         Scanner in = new Scanner(System.in);
  37.         String msg;
  38.         while(true)
  39.         {
  40.             try
  41.             {
  42.                 msg = in.nextLine();
  43.                 mycs.broadcast(msg);
  44.             }
  45.             catch (Exception e)
  46.             {
  47.                 System.out.println("A problem occured!");
  48.             }
  49.         }
  50.     }
  51.    
  52.     public static void main(String[] args)
  53.     {
  54.         String url = "rmi://localhost/ChatServer";
  55.         try
  56.         {
  57.             final ChatRemoteServer cs = (ChatRemoteServer) Naming.lookup(url);            
  58.             final ChatClient client = new ChatClient();
  59.             new Runnable() {
  60.                 public void run() {
  61.                     try {
  62.                         client.ChatClientImp(cs);
  63.                     } catch (RemoteException e){
  64.                         System.out.println(e.getCause());
  65.                     }
  66.                 }
  67.             };
  68.          //   client.run();
  69.         } catch (Exception e) {
  70.             System.out.println("Problem... :"+e);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment