RMIを使用してクライアントマシンをサーバーに接続するプログラムを書いていますが、これまでのところjava.net.ConnectException: Connection refused
.
これが私のコードです。
インターフェース
public interface ServerInterface extends Remote
{
public String getMessage() throws RemoteException;
}
サーバ
public class Server extends UnicastRemoteObject implements ServerInterface
{
int portNumber = 7776;
String ipAddress;
Registry registry;
public Server() throws RemoteException
{
try
{
ipAddress = "192.168.0.104";
System.out.println("IP Address: " + ipAddress + " Port Number: " + portNumber);
registry = LocateRegistry.getRegistry();
registry.rebind("ServerFour", this);
}
catch (RemoteException e)
{
System.out.println("Remote Exception Error");
}
}
public String getMessage() throws RemoteException
{
String output = "Connected to Server";
return output;
}
public static void main(String args[])
{
try
{
Server server = new Server();
}
catch (RemoteException ex)
{
System.out.println("Remote Exception in Main");
}
}
}
クライアント
public class Client
{
public static void main(String[] args) throws NumberFormatException, RemoteException, NotBoundException
{
try
{
ServerInterface server;
Registry registry;
String serverAddress = "192.168.0.104";
String serverPort = "7776";
registry = LocateRegistry.getRegistry(serverAddress, Integer.valueOf(serverPort));
server = (ServerInterface)(registry.lookup("ServerFour"));
String serverMessage = server.getMessage();
System.out.println("Servers Message: " + serverMessage);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
今のところ、Client に ServerInterface のメソッドを呼び出してメッセージを出力させたいだけですが、うまく動作しないようです。クライアントを起動すると、上記の例外メッセージが表示されます。
サーバーを起動すると、次のように返されます。
IP アドレス: client4/127.0.1.1 ポート番号: 1234
アップデート:
ポート番号を 7776 に変更しました Ran rmiregistry 7776 &
これは、サーバーを起動して netstat -anpt http://i.imgur.com/GXnnG.pngを実行すると得られるものです。
クライアント側でこれを取得しています: http://i.imgur.com/aBvW3.png