I worked a simple program
But when you run the client at the command
This error appears
HelloClient exception: java.lang.UnsupportedOperationException: Not supported yet.
this my coded
Interface class
import java.rmi.*;
public interface HelloInterface extends Remote {
public String say() throws RemoteException;
}
implement class
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
*
* #author x
*/
public class HelloServerImpl extends UnicastRemoteObject implements HelloInterface {
private String message;
public HelloServerImpl(String msg)throws RemoteException{
message = msg;
}
#Override
public String say() throws RemoteException {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Server class
import java.rmi.Naming;
/**
*
* #author x
*/
public class HelloServer {
public static void main (String []args ){
try {
Naming.rebind("HELLOSERVER", new HelloServerImpl("Hello word"));
System.out.println("Hello Server is ready.");
} catch (Exception ex) {
System.out.println("Hello server failed: "+ ex);
}
}
}
Client class
import java.rmi.Naming;
/**
*
* #author x
*/
public class HelloClient {
public static void main(String[]args){
HelloInterface hello;
String url = "rmi://localhost/HELLOSERVER";
try {
hello = (HelloInterface)Naming.lookup(url);
System.out.println(hello.say());
} catch (Exception ex) {
System.err.println("HelloClient exception: " + ex);
}
}
}
I am prepared to write the steps but still the same error
why??
Well you wrote this yourself:
#Override
public String say() throws RemoteException {
throw new UnsupportedOperationException("Not supported yet.");
}
Of course it throws an exception. Try to actually return a string:
#Override
public String say() throws RemoteException {
return "hello";
}
Related
I am not able to connect to my websocket server through Java websocket client.Please help!
Apache Tomcat 8.0.26 is being used
Getting exception as "java.io.IOException: An operation was attempted on something that is not a socket.
Client Class
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import javax.websocket.*;
#ClientEndpoint
public class WebSocketClient {
private static Object waitLock = new Object();
protected Session userSession = null;
private MessageHandler messageHandler;
private static void wait4TerminateSignal() {
synchronized (waitLock) {
try {
waitLock.wait();
} catch (InterruptedException e) {
}
}
}
public void Connect(String sServer) throws InterruptedException {
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
System.out.println("Connecting");
WebSocketClient webSocketClient = new WebSocketClient();
userSession = container.connectToServer(webSocketClient, URI.create(sServer));
wait4TerminateSignal();
System.out.println("Connected");
} catch (DeploymentException | IOException e) {
e.printStackTrace();
}
}
/**
* register message handler
*
* #param message
*/
public void addMessageHandler(MessageHandler msgHandler) {
this.messageHandler = msgHandler;
}
/**
* Send a message.
*
* #param user
* #param message
*/
public void sendMessage(String message) {
this.userSession.getAsyncRemote().sendText(message);
}
public void SendMessage(String sMsg) throws IOException {
userSession.getBasicRemote().sendText(sMsg);
}
#OnOpen
public void onOpen(Session session) {
System.out.println("Connected");
}
#OnClose
public void onClose(Session session, CloseReason closeReason) {
}
#OnMessage
public void onMessage(Session session, String msg) {
System.out.println(msg);
}
public void Disconnect() throws IOException {
userSession.close();
}
}
I have a problem with Kryonet server crashing after receiving an object from client.
Server code:
package com.qookie.miner_server;
import java.io.IOException;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.minlog.Log;
import com.qookie.miner_server.Packet.*;
public class MinerServer_Main {
private Server server;
public MinerServer_Main() throws IOException {
this.server = new Server();
RegisterPackets();
server.addListener(new NetworkListener());
server.bind(8888,8888);
server.start();
}
private void RegisterPackets() {
Kryo kryo = server.getKryo();
kryo.register(Packet0LoginRequest.class);
kryo.register(Packet1LoginAnswer.class);
kryo.register(Packet2Message.class);
}
public static void main(String[] args) {
try {
new MinerServer_Main();
} catch (IOException e) {
e.printStackTrace();
}
}
}
And here is a server listener:
package com.qookie.miner_server;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.qookie.miner_server.Packet.*;
public class NetworkListener extends Listener {
public void connected(Connection arg0) {
System.out.println("[SERVER] Someone has connected");
}
public void disconnected(Connection arg0) {
System.out.println("[SERVER] Someone has disconnected");
}
public void received(Connection c, Object o) {
if (o instanceof Packet0LoginRequest) {
Packet1LoginAnswer p = new Packet().new Packet1LoginAnswer();
p.accepted = true;
c.sendTCP(p);
}
if (o instanceof Packet2Message) {
String message = ((Packet2Message) o).msg;
System.out.println("[CLIENT] " + message);
}
}
}
And here is Packet.java file:
package com.qookie.miner_server;
public class Packet {
public class Packet0LoginRequest {
public Packet0LoginRequest() {}
public void init() {
}
};
public class Packet1LoginAnswer {
public boolean accepted;
public Packet1LoginAnswer() {}
public void init() {
}
};
public class Packet2Message {
public Packet2Message() {}
public String msg;
public void init() {
}
};
public Packet() {
}
}
When user connects client sends a Packet0LoginRequest and server sends back Packet1LoginAnswer.
When boolean variable in Packet1LoginAnswer is true, client starts reading from scanner and sending new Packet2Message.
But the server crashes when receiving Packet0LoginRequest.
Here is the crash log:
Server crash log
It think kryonet fails to load your constructor.
Following approach worked from me:
public class Network {
static public final int tcpPort = 54555;
static public final int udpPort = 54777;
// This registers objects that are going to be sent over the network.
static public void register (EndPoint endPoint) {
Kryo kryo = endPoint.getKryo();
kryo.register(ServerResponse.class);
kryo.register(ClientRequest.class);
}
static public class ServerResponse {
...
public ServerResponse(){
super();
}
...
}
static public class ClientRequest {
...
public ClientRequest(){
super();
}
...
}
...
}
We are trying to create a system using Javas RMI. The problem is that a maintained list on the client cannot be accessed from the server using Java RMI. It seems that the RMI connection is handling a copy of the initialized list.
Below is a minimal example using an integer that the client increments every second until it equals 10. The server receives 0 all the time though.
Anyone have any idea what we are doing wrong?
Just run server and the client as a java application.
ServerDefaultImpl.java
package rmi;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class ServerDefaultImpl implements EIServerRemote, Runnable {
ClientRemote client;
private boolean running = true;
public ServerDefaultImpl() {
try {
LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
ServerDefaultImpl server = this;
EIServerRemote stub = (EIServerRemote) UnicastRemoteObject.exportObject(server, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind("test", stub);
} catch (RemoteException e) {
e.printStackTrace();
}
new Thread(this).start();
}
public static void main(String[] args) {
new ServerDefaultImpl();
}
#Override
public void run() {
while (true == running) {
try {
Thread.sleep(1000);
if (null != client) { //Client not connected yet.
int test = client.test();
System.out.println(test);
running = test <= 10;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public void attachClientListener(ClientRemote client) throws RemoteException {
this.client = client;
}
}
EIServerRemote.java
package rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface EIServerRemote extends Remote {
void attachClientListener(ClientRemote client) throws RemoteException;
}
ClientRemote.java
package rmi;
import java.io.Serializable;
import java.rmi.Remote;
public interface ClientRemote extends Remote,Serializable {
int test();
}
ClientDefaultImpl.java
package rmi;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class ClientDefaultImpl implements Runnable,
ClientRemote {
private static final long serialVersionUID = 4846141863099303590L;
protected EIServerRemote server = null;
public int test;
public boolean running = true;
public ClientDefaultImpl(String serverName) {
test = 0;
try {
connect(serverName);
} catch (RemoteException | NotBoundException e) {
e.printStackTrace();
}
new Thread(this).start();
}
public static void main(String[] args) {
new ClientDefaultImpl("test");
}
public void connect(String serverName) throws RemoteException,
NotBoundException {
Registry registry = LocateRegistry.getRegistry();
EIServerRemote s = (EIServerRemote) registry.lookup(serverName);
server = s;
s.attachClientListener((ClientRemote) this);
}
#Override
public void run() {
while (true == running) {
try {
Thread.sleep(1000);
System.out.println(test++);
running = test <= 10;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
#Override
public int test() {
return test;
}
}
It seems that the RMI connection is handling a copy of the initialized list.
That's correct. The list isn't a remote object, so it is passed and returned via serialization.
I'm currently developing a system that loads classes via rmi. This system uses a classloader that communicates with the server in order to get the classes. The code is as follows.
Server:
import rocks.squareRock;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends UnicastRemoteObject
implements RemInterface {
public Server() throws RemoteException {
super();
}
public static void main(String argv[]) {
try {
Server serv = new Server();
Naming.rebind("RockServer", serv);
} catch (Throwable t) {
t.printStackTrace();
}
}
public Class<?> getRockClass(String type) {
if (type.equals("squareRock"))
return squareRock.class;
else
return null;
}
}
Client:
import rocks.Rock;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class Client {
RemInterface reminterface = null;
RockLoader rl = null;
public Client() {
String strName = "rmi://127.0.0.1/RockServer";
try {
reminterface = (RemInterface) Naming.lookup(strName);
rl = new RockLoader(reminterface);
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
loadRock("squareRock");
}
public Rock loadRock(String rock) {
try {
return (Rock) rl.loadClass(rock, false).newInstance();
} catch (Throwable t) {
return null;
}
}
}
Interface:
public interface RemInterface {
public Class<?> getRockClass(String type) throws RemoteException;
}
RockLoader:
import java.io.Serializable;
public class RockLoader extends ClassLoader implements Serializable {
private RemInterface reminterface = null;
public RockLoader(RemInterface reminterface) {
super();
this.reminterface = reminterface;
}
#Override
protected synchronized Class<?> loadClass(String className, boolean resolve)
throws ClassNotFoundException {
try {
return reminterface.getRockClass(className);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
The error I'm getting with this is (client-side):
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: SquareRock
This confuses me, as I'm not unmarshalling a SquareRock instance, but a Class. The only thought I have is that my classloader might be wrong.
It doesn't matter whether it's a Class or an object. The receiving JVM must have that class in its classpath, unless you are using the RMI codebase feature. What you are doing is basically trying to implement the codebase feature yourself. You can't do that.
I've been building an RMI application over the past week and I've hit a roadblock that no amount of googling can seem to help with.
The following code is used to send an object from the server to the client via RMI:
Server code:
import rocks.Rock;
import rocks.squareRock;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends UnicastRemoteObject
implements RemInterface {
public Server() throws RemoteException {
super();
}
public static void main(String argv[]) {
try {
Server serv = new Server();
Naming.rebind("RockServer", serv);
} catch (Throwable t) {
t.printStackTrace();
}
}
public Rock getRock() {
return new squareRock();
}
}
Client code:
import rocks.Rock;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
public class Client {
RemInterface reminterface = null;
public Client() {
String strName = "rmi://127.0.0.1/RockServer";
try {
reminterface = (RemInterface) Naming.lookup(strName);
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public Rock loadRock() {
try {
return reminterface.getRock();
} catch (Throwable t) {
return null;
}
}
}
Interface:
public interface RemInterface {
public Rock getRock() throws RemoteException;
}
In this situation:
The "Rock" class is available in both the Client and Server classpath.
The "Rock" class implements serializable.
The "squareRock" extends class rock and is only available in the server's classpath.
The error I get when trying to call a method using a Rock from loadRock() on the client is as follows:
STDERR: java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: SquareRock
Any help would be appreciated.
You are returning an object of Type rocks.squareRock from the Server. During the de-serialization process at the client, this class will be required in order to create an instance of this class to represent the response from the server. As you've already indicated that the class is available only in the server's classpath, the failure to locate and load the said class causes the exception.
The resolution will be to make the rocks.squareRock class available in the client as well.