SHOW:
|
|
- or go back to the newest paste.
| 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 ChatServer mycs; | |
| 23 | ||
| 24 | public void ChatClientImp(ChatServer 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 | ChatServer cs = (ChatServer) Naming.lookup(url); | |
| 58 | ||
| 59 | - | ChatClient cc = new ChatClient(); |
| 59 | + | new Runnable() {
|
| 60 | - | // new Thread(new ChatClientImp(cs)).start(); |
| 60 | + | public void run() {
|
| 61 | try {
| |
| 62 | new ChatClient(); | |
| 63 | } catch (RemoteException e){
| |
| 64 | System.out.println(e.getCause()); | |
| 65 | } | |
| 66 | } | |
| 67 | }; | |
| 68 | } catch (Exception e) {
| |
| 69 | System.out.println("Problem... :"+e);
| |
| 70 | } | |
| 71 | } | |
| 72 | } |