RMI - Criando HelloWorld

4 respostas
A

Ele fala que a conexão foi negada:

Erro na classe HelloClient -> Connection refused to host: cgt76219; nested exception is: 
	java.net.ConnectException: Connection refused: connect

Tentei escrever localhost ao invés do nome mas nada...

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Hello extends Remote {
	String sayHello() throws RemoteException;
}
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class HelloImpl extends UnicastRemoteObject implements Hello {

	private static final long serialVersionUID = 1L;

	protected HelloImpl() throws RemoteException {
		super();
	}

	@Override
	public String sayHello() throws RemoteException {
		return "Hello World";
	}

}
import java.rmi.Naming;
public class HellClient {
	public static void main(String args[]) {
		// Cria e instala o security manager
		// System.setSecurityManager (new RMISecurityManager () );
		try {
			Hello obj = (Hello) Naming.lookup("rmi://cgt76219/HelloServer");
			System.out.println(obj.sayHello());
		} catch (Exception e) {
			System.out.println("Erro na classe HelloClient -> " + e.getMessage());
		}
		System.exit(0);
	}
}
import java.rmi.Naming;
public class HelloServer {
	public static void main(String args[]) {
		// Cria e instala o Security Manager
		// System.setSecurityManager(new RMISecurityManager());
		try {
			HelloImpl obj = new HelloImpl();
			Naming.rebind("HelloServer", obj);
			System.out.println("Hello Server pronto.");
		} catch (Exception e) {
			System.err.println("Erro na classe HelloServer " + e.getMessage());
		}
	}
}

4 Respostas

georgesq

segue este e testa.

http://download.oracle.com/javase/tutorial/rmi/server.html

[s]

BobCycler

Substitua a url “rmi://cgt76219/HelloServer” da classe HellClient para “rmi://127.0.0.1/HelloServer” ou “rmi://localhost/HelloServer” e refaça o teste.

maior_abandonado

acho que faltou é você colocar a porta na sua url (provavelmente 1099).

olhei bem por alto seu código, se não funcionar depois do uma olhada com mais cuidado.

se o nome do seu pc é “cgt76219”, tanto faz usar isso, localhost ou 127.0.0.1…

BobCycler

Realmente, pela mensagem de erro apresentada você está tentando executar a classe cliente antes de iniciar o servidor.

Esse tópico pode ajuda-lo a resolver este problema.

Criado 3 de março de 2011
Ultima resposta 12 de abr. de 2011
Respostas 4
Participantes 4