As I'm trying to achieve my first RMI program, I'm having troubles testing it.
I compiled my programs and tried to launch my server but an error then occurs.
About my server: 1 interface + 1 class implementing it + my server class
Interface
package src;
import java.rmi.*;
public interface Information extends Remote {
// methods than can be called remotly
public String getInformation() throws RemoteException;
}
Class Implementing it
package src;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
public class InformationImpl extends UnicastRemoteObject implements Information {
protected InformationImpl() throws RemoteException {
super();
}
public String getInformation() throws RemoteException {
System.out.println("method getInformation()");
return "hello";
}
}
My server class :
package src;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class LanceServeur {
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(1099);
InformationImpl informationImpl = new InformationImpl();
Naming.rebind("MyServer", informationImpl);
System.out.println("Server launched");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
And my client class :
package src;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
public class lanceClient {
public static void main(String[] args) {
System.out.println("Lancement du client");
try {
Remote r = Naming.lookup("rmi://localhost:1099/TestRMI");
System.out.println(r);
if (r instanceof Information) {
String s = ((Information) r).getInformation();
System.out.println("chaine renvoyee = " + s);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
System.out.println("end of client");
}
}
If you want to see the correct lines, check my github it's easier :
https://github.com/TLprojet/MUD_Java
About the error, here it is:
java.rmi.ConnectIOException: Exception creating connection to: 0.0.21.214; nested exception is:
java.net.SocketException: Invalid argument or cannot assign requested address
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:631)
at java.rmi/sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:209)
at java.rmi/sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:196)
at java.rmi/sun.rmi.server.UnicastRef.newCall(UnicastRef.java:338)
at java.rmi/sun.rmi.registry.RegistryImpl_Stub.rebind(RegistryImpl_Stub.java:147)
at java.rmi/java.rmi.Naming.rebind(Naming.java:177)
at src.LanceServeur.main(LanceServeur.java:16)
Caused by: java.net.SocketException: Invalid argument or cannot assign requested address
at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
at java.base/java.net.Socket.connect(Socket.java:591)
at java.base/java.net.Socket.connect(Socket.java:540)
at java.base/java.net.Socket.<init>(Socket.java:436)
at java.base/java.net.Socket.<init>(Socket.java:213)
at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createSocket(TCPDirectSocketFactory.java:40)
at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 6 more
Related
I am getting the following error:
Exception in thread "main" java.rmi.NotBoundException: Server
at java.rmi/sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:234)
at java.rmi/sun.rmi.registry.RegistryImpl_Skel.dispatch(RegistryImpl_Skel.java:133)
at java.rmi/sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:468)
at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:298)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:562)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:796)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:677)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:676)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
at java.rmi/sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:303)
at java.rmi/sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:279)
at java.rmi/sun.rmi.server.UnicastRef.invoke(UnicastRef.java:380)
at java.rmi/sun.rmi.registry.RegistryImpl_Stub.lookup(RegistryImpl_Stub.java:123)
at client.RMIClient.startClient(RMIClient.java:17)
at client.FahrradClient.main(FahrradClient.java:12)
Can anybody look at my code and tell me where I went wrong? I'd be more than grateful. Would even send a small tip or something lmao I am that desperate.
package server;
import shared.RMI_Interface;
import java.io.IOException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class FahrradServer {
public static void main(String[] args) throws IOException, AlreadyBoundException {
RMI_Interface server = new ConfigImpl();
Registry registry = LocateRegistry.createRegistry(1099);
Runtime.getRuntime().exec("rmiregistry 1099");
registry.bind("Server", server);
System.out.println("Server started");
}
}
package client;
import shared.Fahrrad;
import shared.RMI_Interface;
import java.rmi.*;
import java.rmi.registry.*;
public class RMIClient {
private RMI_Interface server;
public RMIClient() {}
public void startClient() throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
server = (RMI_Interface)registry.lookup("Server");
}
public Fahrrad configureFahrrad(String lenkertyp, String material, String schaltung, String griff ) {
Fahrrad result = null;
try {
result = server.configureFahrrad(lenkertyp, material, schaltung, griff);
} catch (RemoteException e) {
e.printStackTrace();
throw new RuntimeException("Could not contact server");
}
return result;
}
}
package server;
import shared.Fahrrad;
import shared.RMI_Interface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ConfigImpl implements RMI_Interface {
public void ConfigImpl() throws RemoteException {
UnicastRemoteObject.exportObject(this, 0);
}
#Override
public Fahrrad configureFahrrad(String lenkertyp, String material, String schaltung, String griff ) throws RemoteException {
Fahrrad f = new Fahrrad();
f.setLenkertyp(lenkertyp);
f.setGriff(griff);
f.setMaterial(material);
f.setSchaltung(schaltung);
if (!lenkertyp.equals( "Faltbarlenker") && !lenkertyp.equals( "Rennradlenker") && !lenkertyp.equals( "Bullhornlenker")) {
throw new IllegalArgumentException("Ungültiger Lenktypinput");
}
if (!material.equals( "Aluminium") && !material.equals( "Stahl") && !material.equals( "Kunststoff")){
throw new IllegalArgumentException("Ungültiges Material!");
}
if (!schaltung.equals("Kettenschaltung") && !schaltung.equals( "Tretlagerschaltung") && !lenkertyp.equals( "Nebenschaltung")) {
throw new IllegalArgumentException("Ungültige Schaltung!");
}
if (!griff.equals( "Kunststoffgriff") && !griff.equals( "Ledergriff") && !lenkertyp.equals( "Schaumstoffgriff")) {
throw new IllegalArgumentException("Ungültige Schaltung!");
}
if (lenkertyp.equals("Faltbarlenker") || lenkertyp.equals("Rennradlenker")) {
if (!material.equals( "Aluminium") && !material.equals( "Kunststoff")) {
throw new IllegalArgumentException(lenkertyp + " kann nur aus Aluminium oder Kunststoff bestehen.");
}
}
if (material.equals("Stahl")) {
if (!schaltung.equals("Kettenschaltung")) {
throw new IllegalArgumentException("Materialtyp "+ material + " kann nur Kettenschaltung haben!");
}
}
if (material.equals("Kunststoff")) {
if (griff.equals("Kunststoffgriff")) {
throw new IllegalArgumentException("Nur Kunststoffmaterial kann Kunststoffgriff haben!");
}
}
if (lenkertyp.equals( "Bullhornlenker") || lenkertyp.equals("Faltbarlenker")) {
if (griff.equals("Ledergriff")) {
throw new IllegalArgumentException("Nur Rennladlenker können Ledergriffe haben!");
}
}
return f;
}
}
package server;
import shared.RMI_Interface;
import java.io.IOException;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class FahrradServer{
public static void main(String[] args) throws IOException, AlreadyBoundException {
RMI_Interface server = new ConfigImpl();
Registry registry = LocateRegistry.createRegistry(1099);
Runtime.getRuntime().exec("rmiregistry 1099");
registry.bind("Server", server);
System.out.println("Server started");
}
}
package shared;
public class Fahrrad {
public String lenkertyp;
public String material;
public String schaltung;
public String griff;
public void setLenkertyp(String lenkertyp){
this.lenkertyp=lenkertyp;
}
public String getLenkertyp() {
return lenkertyp;
}
public void setMaterial(String material){
this.material=material;
}
public String getMaterial() {
return material;
}
public void setSchaltung(String schaltung) {
this.schaltung=schaltung;
}
public String getSchaltung() {
return schaltung;
}
public void setGriff(String griff){
this.griff=griff;
}
public String getGriff() {
return griff;
}
}
package shared;
import java.rmi.Remote;
import java.rmi.RemoteException;
// Creating Remote interface for our application
public interface RMI_Interface extends Remote {
public Fahrrad configureFahrrad(String lenkertyp, String material, String schaltung, String griff ) throws RemoteException;
}
I know the code isn't perfect. I will change it but the most important thing is to get the client and server run independently. My client sadly doesn't run.
The steps to follow when creating a RMI application.
Create the "remote" interface.
Your interface RMI_Interface is OK. Nothing to do here.
Create a class that implements the remote interface.
I made the constructor empty. (The rest of the code is unchanged.)
public ConfigImpl() throws RemoteException {
// Empty.
}
Create a RMI server. Here I made a few changes.
package server;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import shared.RMI_Interface;
public class FahrradServer {
public static void main(String[] args) {
try {
ConfigImpl server = new ConfigImpl();
RMI_Interface stub = (RMI_Interface) UnicastRemoteObject.exportObject(server, 0);
Registry registry = LocateRegistry.createRegistry(1099);
registry.bind("Server", stub);
System.out.println("Server started");
}
catch (AlreadyBoundException | RemoteException x) {
x.printStackTrace();
}
}
}
Note that calling method createRegistry() will launch the rmiregistry so no need for this line of your code:
Runtime.getRuntime().exec("rmiregistry 1099");
Run the server, i.e. java server.FahrradServer
(I assume you know the correct command that you need to actually launch your server.)
Note that the server program will run forever – unless you kill it.
Write the client. The client will run in a separate JVM so the client needs a main() method so that you can launch it via the java command.
package client;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import shared.Fahrrad;
import shared.RMI_Interface;
public class RMIClient {
private RMI_Interface server;
public void startClient() throws RemoteException, NotBoundException {
Registry registry = LocateRegistry.getRegistry(1099);
server = (RMI_Interface) registry.lookup("Server");
}
public Fahrrad configureFahrrad(String lenkertyp,
String material,
String schaltung,
String griff) throws RemoteException {
return server.configureFahrrad(lenkertyp, material, schaltung, griff);
}
public static void main(String[] args) {
RMIClient client = new RMIClient();
try {
client.startClient();
Fahrrad f = client.configureFahrrad("Faltbarlenker",
"Aluminium",
"Kettenschaltung",
"Kunststoffgriff");
System.out.println("Fahrrad: " + f);
}
catch (RemoteException | NotBoundException x) {
x.printStackTrace();
}
}
}
Since the return value of the remote interface method is a class that you wrote, that class must implement interface Serializable, i.e.
public class Fahrrad implements java.io.Serializable
Now you can launch your RMI client, e.g. java client.RMIClient
Refer to the RMI trail of Oracle's java tutorials.
I am very new to Java and the RMI system. I am following a tutorial but I am unsure why I keep getting the following errors[1]
[1]: https://i.stack.imgur.com/xeYTn.png
I have attached the code (taken directly from the tutorial here: https://docs.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html)
I have tried:
removing any lines with 'package'
changing classpath variables
reinstalling java and javac
setting classpath in the 'rmiregistry &' command
Any help would be appreciated
edit: forgot to attach the code.
Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
Client.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {
}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
Server.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {
}
public String sayHello() {
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject((Remote) obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
You have to run the command rmiregistry & in the folder the code is compiled into. In this case, "files"
the "getting started" tutorial didn't mention that.
Server:
package server;
import java.rmi.*;
public interface iCSServer extends Remote
{
public int Findnumber(int num) throws RemoteException;
}
package server;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
And
public class CSServer extends UnicastRemoteObject implements iCSServer {
private static final long serialVersionUID = 01; // version 01
int N=1000; // number of ids
int[] Ids=new int[N]; // data structure for storing the Ids
public CSServer() throws RemoteException
{
}
public void Add_Ids() // add ids to data
{
for(int i=0;i<N;i++)
{
Ids[i]=i+ (i+1)*10;
}
}
public int Findnumber(int num) throws RemoteException
{
for(int i=0;i<N;i++)
{
if(num==Ids[i])
{
return i;
}
}
return -1;
}
public static void main(String[] args) throws RemoteException {
System.setProperty("java.rmi.server.hostname","127.0.0.1");
LocateRegistry.createRegistry(1099);
// System.out.println(LocateRegistry.getRegistry(1099).toString());
CSServer obj=new CSServer();
obj.Add_Ids();
try {
Naming.rebind ("CSServer", obj);
System.out.println ("CS218 Server is running.");
}
catch (Exception e) {
System.out.println ("CS218 server cannot run: " + e);
}
}
}
Server starts and runs fine.
Client:
package client;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.*;
public interface iCSServer extends Remote
{
public int Findnumber(int num) throws RemoteException;
}
And
package client;
import java.rmi.Naming;
import java.rmi.registry.*;
public class clientApp {
public static void main(String[] args)
{
try{
iCSServer obj = (iCSServer) Naming.lookup("//localhost:1099/CSServer");
//Registry registry = LocateRegistry.getRegistry("127.0.0.1");
//iCSServer obj=(iCSServer) registry.lookup("CSServer");
System.out.println("pass");
System.out.println(obj.Findnumber(100));
}
catch(Exception ex)
{
System.out.println("fail");
System.out.println(ex.getMessage());
}
}
}
However, when I run the client I get this error:
fail
error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: server.iCSServer (no security manager: RMI class loader disabled)
then I added a jar'd version of server.ICSServer, I believe that fixed that issue. However, I ran it again and now I get this error:
fail
com.sun.proxy.$Proxy0 cannot be cast to client.iCSServer
Any ideas on how to fix?
You have renamed the interface by putting it into a different package. It has to be the same.
File: SelectServerIntf.java:
import java.rmi.*;
import java.util.*;
public interface SelectServerIntf extends Remote{
HashMap executeSelect() throws RemoteException;
}
File: SelectServerImpl.java:
import java.rmi.*;
import java.sql.*;
import java.rmi.server.*;
import java.util.*;
public class SelectServerImpl extends UnicastRemoteObject implements SelectServerIntf {
public SelectServerImpl() throws RemoteException
{
}
public HashMap executeSelect() throws RemoteException
{
String DRIVER = "com.mysql.jdbc.Driver";
String url ="jdbc:mysql://localhost:3306/abc2";
String Query="select * from student";
Connection con=null;
Statement stat=null;
HashMap hm=null;
try
{
Class.forName(DRIVER);
}
catch(ClassNotFoundException cn)
{
System.out.println("ClassNotFound"+cn);
}
try
{
con= DriverManager.getConnection(url,"root","root");
stat=con.createStatement();
ResultSet rs=stat.executeQuery(Query);
hm=new HashMap();
while(rs.next())
{
int rno=rs.getInt(1);
String name=rs.getString(2);
hm.put(new Integer(rno),name);
}
con.close();
}
catch(SQLException se)
{
System.out.println("SQLException"+se);
}
return(hm);
}
}
File: SelectServer.java:
import java.rmi.*;
import java.net.*;
public class SelectServer {
public static void main(String args[])
{
try
{
SelectServerImpl sip=new SelectServerImpl();
Naming.rebind("SELECT-SERVER", sip);
}
catch(Exception e)
{
System.out.println("Exception:"+e);
}
}
}
File: SelectClient.java:
import java.rmi.*;
import java.util.*;
import java.net.*;
public class SelectClient {
public static void main(String args[])
{
String rmiurl="rmi://"+args[0]+"/SELECT-SERVER";
try
{
SelectServerIntf sit=(SelectServerIntf)Naming.lookup(rmiurl);
HashMap hm2=sit.executeSelect();
int sz=hm2.size();
for(int i=1;i<sz;i++)
{
if(hm2.containsKey(new Integer(i)))
System.out.println(i+":"+hm2.get(new Integer(i)));
}
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
}
The above RMI program on execution gives
"Class not found Exception:com.mysql.jdbc.Driver"
SQLException:No Suitable Driver found for jdbc:mysql://localhost:3306/abc2
where abc2 is my database containing respective tables.
The above given connection url and drivers works properly for every code but acccept this rmi.
Experts do you find any modification??
You need to put mysql jdbc driver jars in your server's classpath. It will enable your server to load driver class while calling executeSelect()
hi i am using this codes for rmi
RmiServer.java
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
public class RmiServer extends java.rmi.server.UnicastRemoteObject
implements ReceiveMessageInterface
{
int thisPort;
String thisAddress;
Registry registry; // rmi registry for lookup the remote objects.
// This method is called from the remote client by the RMI.
// This is the implementation of the gReceiveMessageInterfaceh.
public void receiveMessage(String x) throws RemoteException
{
System.out.println(x);
}
public RmiServer() throws RemoteException
{
try{
// get the address of this host.
thisAddress= (InetAddress.getLocalHost()).toString();
}
catch(Exception e){
throw new RemoteException("can't get inet address.");
}
thisPort=3232; // this port(registryfs port)
System.out.println("this address="+thisAddress+",port="+thisPort);
try{
// create the registry and bind the name and object.
registry = LocateRegistry.createRegistry( thisPort );
registry.rebind("rmiServer", this);
}
catch(RemoteException e){
throw e;
}
}
static public void main(String args[])
{
try{
RmiServer s=new RmiServer();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
RmiClient.java
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
public class RmiClient
{
static public void main(String args[])
{
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress=args[0];
String serverPort=args[1];
String text=args[2];
System.out.println("sending "+text+" to "+serverAddress+":"+serverPort);
try{
// get the �gregistry�h
registry=LocateRegistry.getRegistry(
serverAddress,
(new Integer(serverPort)).intValue()
);
// look up the remote object
rmiServer=
(ReceiveMessageInterface)(registry.lookup("rmiServer"));
// call the remote method
rmiServer.receiveMessage(text);
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
e.printStackTrace();
}
}
}
ReceiveMessageInterface.java
import java.rmi.*;
public interface ReceiveMessageInterface extends Remote
{
public void receiveMessage(String x) throws RemoteException;
}
This works fine normally , but when the a computer is connected to internet through mobile or it shares internet from other pc it doesn't work
I get this error.
java.net.connectexception connection timeout
when i tried to telnet it fails to connect but when i try to run this program that pc
to my pc it works.
Please let me know how to solve this issue.
Sounds like a firewall or proxy server issue.