我正在编写一个程序,使用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拒绝连接?然后服务器没有启动并运行,或者访问被禁止。 –