2013-11-01 2 views
1

Я написал приложение чата Java RMI. Существует четыре класса и два интерфейса. Вот они:Получение NotBoundException при написании приложения чата Java RMI: -/

ChatClient

import java.rmi.RemoteException; 
import java.rmi.server.UnicastRemoteObject; 
import java.util.Scanner; 
com.za.tutorial.rmi.server.ChatServerIF; 

public class ChatClient extends UnicastRemoteObject implements ChatClientIF,Runnable { 
private ChatServerIF chatServer; 
private String name = null; 

protected ChatClient(String name, ChatServerIF chatServer) throws RemoteException { 
    this.name = name; 
    this.chatServer = chatServer; 
    chatServer.registerChatClient(this); 
} 

public void retrieveMessage(String message) throws RemoteException { 
    // TODO Auto-generated method stub 
    System.out.println(message); 
} 

public void run() { 
    Scanner scanner = new Scanner(System.in); 
    String message; 
    while(true){ 
     message = scanner.nextLine(); 
     try { 
      chatServer.broadcastMessage(name + " : " + message); 
     } catch (RemoteException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    } 

}

ChatClientDriver

import java.net.MalformedURLException; 
import java.rmi.Naming; 
import java.rmi.NotBoundException; 
import java.rmi.RemoteException; 
import com.za.tutorial.rmi.server.ChatServerIF; 

public class ChatClientDriver { 
public static void main(String[] args) throws MalformedURLException, RemoteException, NotBoundException { 

String chatServerURL = "rmi://localhost/RMIChatServer"; 
ChatServerIF chatServer = (ChatServerIF) Naming.lookup(chatServerURL); 
new Thread(new ChatClient(args[0],chatServer)).start(); 
} 
} 

ChatClientInterface

import java.rmi.Remote; 
import java.rmi.RemoteException; 

public interface ChatClientIF extends Remote { 
void retrieveMessage(String message) throws RemoteException; 
} 

ChatServer

import java.rmi.RemoteException; 
import java.rmi.server.UnicastRemoteObject; 
import java.util.ArrayList; 
import com.za.tutorial.rmi.client.ChatClientIF; 

public class ChatServer extends UnicastRemoteObject implements ChatServerIF { 


private ArrayList<ChatClientIF> chatClients; 
protected ChatServer() throws RemoteException { 
    chatClients = new ArrayList<ChatClientIF>(); 
    } 

public synchronized void registerChatClient(ChatClientIF chatClient) 
     throws RemoteException { 
    this.chatClients.add(chatClient); 
} 

public synchronized void broadcastMessage(String message) throws RemoteException { 
    int i = 0; 
    while(i < chatClients.size()){ 
     chatClients.get(i++).retrieveMessage(message); 
    } 
} 
} 

ChatServerDriver

import java.net.MalformedURLException; 
import java.rmi.Naming; 
import java.rmi.RemoteException; 

public class ChatServerDriver { 

public static void main(String[] args) throws RemoteException, MalformedURLException { 
Naming.rebind("RMIChatServer", new ChatServer()); 
} 
} 

ChatServerInterface

import java.rmi.Remote; 
import java.rmi.RemoteException; 
import com.za.tutorial.rmi.client.ChatClientIF; 

public interface ChatServerIF extends Remote { 
void registerChatClient(ChatClientIF chatClient) throws RemoteException; 
void broadcastMessage(String message) throws RemoteException; 
} 

Когда я запускаю его на Commando, прежде всего, я запускаю rmic ChatClient и ChatServer, а затем rmiregistry. Затем я запускаю chatServerDriver, который работает отлично. после этого, когда я запускаю chatClientDriver с именем, я получаю следующую ошибку, я не понимаю, почему:/Могу ли я получить какое-либо решение для этого?

Спасибо :)

Exception in thread "main" java.rmi.NotBoundException: RMIChatServer 
    at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:136) 
    at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source) 
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:409) 
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267) 
    at sun.rmi.transport.Transport$1.run(Transport.java:177) 
    at sun.rmi.transport.Transport$1.run(Transport.java:174) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173) 
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
    at java.lang.Thread.run(Thread.java:722) 
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source) 
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source) 
    at sun.rmi.server.UnicastRef.invoke(Unknown Source) 
    at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source) 
    at java.rmi.Naming.lookup(Unknown Source) 
    at com.za.tutorial.rmi.client.ChatClientDriver.main(ChatClientDriver.java:15) 

ответ

1

Это также выглядит как у вас есть другой адрес в Пересвяжите к тому, что используется клиентом для подключения.

Naming.rebind("//localhost/RMIChatServer", new ChatServer()); 

На примере следующей страницы Википедии есть пример реализации, который может стоить сравнить с вашим кодом. http://en.wikipedia.org/wiki/Java_remote_method_invocation

Обратите внимание, что с помощью Java 1.5+ вам не нужно использовать РЦМП больше видеть Do we really need to create Stub in java RMI?

+0

нормально, я буду проверить это :) ти – user2855719

Смежные вопросы