• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Java RMI

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

RedDragonXXX

Senior RAM Pornographer
Joined
Mar 3, 2005
Location
Jacksonville, FL
Is anyone here familiar with RMI concept of connecting Client computer to a remote host/Server (NOT localhost)?

I've read ton of tutorials, posted on many forums, tried every option possible under the Sun... and can't get the connection made between my Client and Server machines that run on Linux. I've tried TON of code and nothing seems to work. If anyone is familiar with this please let me know and I can post code if necessary.
 
We run RMI, but just in some special cases. If asyncronius messages suffice (as they mostly do for us) we use activemq for messaging.

So it works with localhost and two separate jvms? Iptables? Selinux? Any stacktraces?
 
Yes, I got it to work on localhost but for the life of me I can't get it to work with a remote machine.

Here is what I'm working with right now, but no luck. If anyone can figure out how this works I will be grateful to you forever! I just need to make a connection between Client and Server (using RMI) machines that live on two separate IP's.

Client
Code:
public static void main(String[] args) throws NumberFormatException, RemoteException, NotBoundException 
    {
    	ServerInterface server;
        Registry registry;
	String serverAddress = args[0];
	String serverPort = args[1];
	String text = args[2];
	
	System.out.println("Sending: " + text + " to: " + serverAddress + ":" + serverPort);

	if (System.getSecurityManager() == null)
		System.setSecurityManager(new SecurityManager());

	try
    	{
        	registry = LocateRegistry.getRegistry(serverAddress, (new Integer(serverPort)).intValue());

		server = (ServerInterface)(registry.lookup("rmiServer"));

		server.getMessage(text);
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
		System.exit(1);
    	}
    	

	}


Server
Code:
public class Server extends UnicastRemoteObject implements ServerInterface 
{
	public void getMessage(String x) throws RemoteException
	{
		System.out.println(x);
	}

	public Server() throws RemoteException
	{
		if (System.getSecurityManager() == null)
			System.setSecurityManager(new SecurityManager());		

		try
		{
			
			ServerInterface engine = new Server();
			ServerInterface stub = (ServerInterface) UnicastRemoteObject.exportObject(engine, 7556);			
			Registry registry = LocateRegistry.getRegistry();
			registry.rebind("rmiServer", stub);

			System.out.println("Interface Bound");
		}
		catch (Exception e)
		{
			System.out.println("Remote exception");
		}
	}

	public static void main(String args[]) throws AlreadyBoundException
	{
		try
		{
			Server server = new Server();
			
		}
		catch (RemoteException ex)
		{
			System.out.println("Remote Exception in Main");
			System.exit(1);
		}
		
	}
 
}

ATM, when I start the Server it hangs for a second and returns this:

PH21l.png
 
Well got a quick update. After 2 weeks of trying to figure this out I finally got it working. Problem was my Client was trying to connect to Server through localhost IP the whole time with 127.0.1.1 IP no matter what I did. I was basically rewriting my code for nothing this whole time and all I had to do was this:

Code:
System.setProperty("java.rmi.server.hostname", "192.168.0.104");

Someone over at Stack Overflow suggested this to me and it worked like a charm.

I feel like I dumped this huge monkey of my back :clap:
 
Back