我正在編寫一個程序,使用RMI將客戶機連接到服務器,到目前爲止,我一直在收到java.net.ConnectException: Connection refused
。使用Java RMI建立連接
這是我的代碼;
接口
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");
}
}
}
客戶
現在我只想客戶端上的ServerInterface方法調用,並打印出它的消息,但我似乎無法得到它的工作。當我啓動客戶端時,出現上面顯示的異常消息。
當我啓動服務器它返回:
IP地址:client4/127.0.1.1端口號:1234
更新:
我已經改變了端口號7776 冉rmiregistry的7776 &
這是我所得到的,當我啓動服務器和運行netstat -anpt http://i.imgur.com/GXnnG.png
現在在客戶端上我得到這樣的: http://i.imgur.com/aBvW3.png
防火牆服務器上的阻塞端口1234?你可以telnet服務嗎? –
它做同樣的事情。 – Nick
請澄清「它做同樣的事情」。如果您使用telnet拒絕連接?然後服務器沒有啓動並運行,或者訪問被禁止。 –