これはRMIに関連する質問です。私は4つの.classファイルを持っています。それは実装、クラスserver、クラスclientです。CMDで起動していないクラスの内容がどのように出力されるのですか
のrmiregistryを起動した後、私は似ているサーバを起動します。私は、関数を呼び出すリモートオブジェクトの参照を使用して
import java.rmi.Naming;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Client {
public static void main(String args[]) {
try {
to_Server_Intf s_Intf = (to_Server_Intf)Naming.lookup("rmi://127.0.0.1/Server");
do {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String message_to_send = null;
System.out.println("===Enter your message===");
message_to_send = br.readLine();
// sending a message to the server
String reply = s_Intf.send(message_to_send);
System.out.println(reply);
}while(true);
}catch(Exception exc) {
System.out.println(exc);
}
}
}
:
import java.rmi.Naming;
public class Server {
public static void main(String args[]) {
try {
to_Server_Impl toServer = new to_Server_Impl();
Naming.rebind("Server", toServer);
} catch(Exception exc) {
System.out.println(exc);
}
}
}
そしてiはコードでクライアントを起動そのクラスのsend
は、そのコードは次のようになります。
import java.rmi.*;
import java.rmi.server.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class to_Server_Impl extends UnicastRemoteObject implements to_Server_Intf{
public to_Server_Impl() throws Exception{ }
@Override
public String send(String str){ // this function receives the message from the Client.
// this method indirectly replies,by calling reply and then returning the replies string
String receivedString = str;
System.out.println(receivedString);
System.out.println();
String reply = reply();
return reply;
}
@Override
public String reply() { // this function replies the Client
String replyString = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
replyString = br.readLine();
}catch(Exception exc) {
System.out.println(exc);
}
return replyString;
}
}
上記のクラスのメソッドを上記クラスのprint
のステートメントで呼び出すと、メッセージは "server
"ウィンドウに表示されます。そんなことがあるものか ?
クライアントウィンドウ:(クライアントがメッセージを送信している)
サーバーウィンドウ:(メッセージは、サーバ・ウィンドウに印刷されます(コールがクラスto_Server_Impl
の機能に向けました)。どうやって?)
私はクラス 'to_Server_Impl'のsendメソッドを呼び出しました。それは 'Server'コンソールにどのように現れますか? –
to_Server_Implは、リモートオブジェクトのインタフェースの実装です。クライアントでは、Naming.lookupを使用してリモートオブジェクトを検索してからメソッドを呼び出します。このメソッドは実際にto_Server_Implによってサーバー上で実行されます。そのため、メッセージはサーバーに出力されます。 – Tudor
RMIの仕組みについての説明が必要ですか? – Tudor