I programmed a client/server application using Java RMI. When I run this application locally (with the IP-String in both client and server set to local IP), the script works fine.
Client Code:
public class ClientMain {
public static void main(String[] args) {
String ip = "123.123.123.123"; // (my server's IP)
IUserHandler userHandler;
try {
userHandler = (IUserHandler) Naming.lookup("//"+ip+"/Server");
System.out.println(userHandler.attachUser(new User("John")));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Server Code:
public class MainServer {
public static void main(String[] args) {
String ip = "123.123.123.123"; // (my server's IP)
System.out.println("Start Server at " + ip);
try {
System.setProperty("java.rmi.server.hostname",ip);
LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
Naming.rebind("Server",new UserHandler());
System.out.println("Server is running");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Now the interfaces and classes of User and UserHandler, which is transported to the server:
Interface IUser:
public interface IUser extends Remote {
public String getUserName() throws RemoteException;
public void setUserName(String name) throws RemoteException;
}
Interface IUserHandler:
public interface IUserHandler extends Remote {
public int attachUser(IUser user) throws RemoteException;
public int detachUser(String name) throws RemoteException;
}
Class UserHandler:
public class UserHandler extends UnicastRemoteObject implements IUserHandler {
public UserHandler() throws RemoteException {}
@Override
public int attachUser(IUser user) throws RemoteException {
System.out.println(user.getUserName() + " logged in");
return 1;
}
@Override
public int detachUser(String name) throws RemoteException{
return 1;
}
}
Class User:
public class User extends UnicastRemoteObject implements IUser {
private String name;
public User(String name) throws RemoteException {
super();
this.name = name;
}
@Override
public String getUserName() {
return name;
}
@Override
public void setUserName(String name) {
this.name = name;
}
}
The server-application should run on an Windows Server 2012 machine. When I run the generated jar on the server, it starts up fine.
So, in class UserHandler there is the following line:
System.out.println(user.getUserName() + " logged in");
When only printing a String without any user Object, the printed line appears prompt after executing the method in the client.
When only the println with user (but without method call getUserNAme()) is printed, it needs 10-15 secs to print.
With the above statement, nothing happens and I get a timeout exception.
In the testing environment I openend all ports of TCP to ensure the messages arrive.
Can anybody help me out please? Thank you!
You're using callbacks, which was implies that you're opening a connection to the client, which implies that the client's firewall must be open.
I strongly suggest you get rid of the callback and make User
a Serializable object.