Java Rmi Exception - java

I want to build a RMI server, I tried like this
package first_project;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
public class Server extends UnicastRemoteObject implements ServerInterface {
// list for know users
protected ArrayList<ClientInterface> clients = new ArrayList<ClientInterface>();
public Server() throws RemoteException {}
//logged in clients get a notification that a new user has joined the chat
// remote reference to the new client is added to the ArrayList.
public void login(ClientInterface client, String nickname) throws RemoteException {
broadcastMessage("--> " + nickname + " is entering the chatroom", "");
clients.add(client);
}
// used for broadcasting an incoming message
// and the nickname of its sender to all currently logged in clients .
//remote call of the method getMessage which is
public void broadcastMessage(String message, String nickname) throws RemoteException {
for (int i = 0; i < clients.size(); i++) {
ClientInterface c = clients.get(i);
try {
c.getMessage(message, nickname);
} catch (RemoteException e) {
logout(c);
i = i - 1;
} }}
//remove user
public void logout(ClientInterface client) {
clients.remove(client);
}
public static void main(String[] args) {
try {
Naming.rebind("Server", new Server());
System.out.println("Server is ready");
} catch (Exception e) {
e.printStackTrace();
}
}
}
but i dont know why I got this exception:
java.rmi.ConnectException: Connection refused to host: 192.168.1.3; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:160)
at first_project.Server.main(Server.java:47)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
... 6 more
what am I doing wrong please?
thanks in advance

Have u started the registry. Try it after starting the registry
start rmiregistry for windows
rmiregistry & for Linux/Unix

Related

Why I try to run Client.jar on Another Computer but receive Connection refused" or "Connection timed out

I spent more than one day , but still can't figure out how can I resolve the problem. Every time I shut down the firewall on my server, my other computer can successfully run Client.jar.
I check the firewall on my server PC. I am sure I opened the port "4888" and "6151", but still can't run normally.
I don't know how "W.getSocketFactory()" works.
System.out.println("創造"+ W.getSocketFactory()); → 創造 null (Is it an issue?)
Is there some problems on my codes or somethings?
Below is my server code:
public class RemoteServer extends UnicastRemoteObject implements MyRemote{
public static void main(String[] args)
{
try {
RMILocalSocketFactory W = new RMILocalSocketFactory(6151);
System.out.println("創造"+ W.getSocketFactory());
RMISocketFactory.setSocketFactory(W);
LocateRegistry.createRegistry(4888);
System.setProperty("java.rmi.server.hostname","175.183.49.139");
MyRemote Server = new RemoteServer();
Naming.rebind("rmi://175.183.49.139:4888/Remote_Hello!", Server);
} catch (Exception e) {
e.printStackTrace();
}
}
RemoteServer() throws RemoteException {}
public String SayHello()
{
System.out.println("RMI Connection");
return "you Connected Server\n" + "Server says , Hey!";
}
}
class RMILocalSocketFactory extends RMISocketFactory {
private int dataPort;
public RMILocalSocketFactory(int dataPort1){
this.dataPort = dataPort1;
}
public Socket createSocket(String host, int port) throws IOException {
String a = "175.183.49.139";
Socket check = new Socket(a = host, port);
return check;
}
public ServerSocket createServerSocket(int port) throws IOException {
return new ServerSocket(port);
}
}
(Client) the one part of code:
try {
LocateRegistry.getRegistry(4888);
MyRemote Service = (MyRemote)Naming.lookup("rmi://175.183.49.139:4888/Remote_Hello!");
String Say= Service.SayHello();
Area.append(Say);
} catch (Exception e) {
e.printStackTrace();
}
```[Problem Picture:][1]
[1]: https://i.stack.imgur.com/LkK5T.jpg
I resolved my problem by myself.
I finally found my java.exe on firewall is blocked.
That is the reason why I can't receive another request while firewall is on.
And make sure both port must be open on firewall as well.

RMI client-server NotBoundException

I am trying to run the rmi tutorial on oracles' website. I am able to run the server, but I receive an error running the client. The error I receive when try to run the cleint is a noNotBoundException exception. How do I fix this error?
Below is the code and exception
Exception
Client exception: java.rmi.NotBoundException: Hello
java.rmi.NotBoundException: Hello
at sun.rmi.registry.RegistryImpl.lookup(RegistryImpl.java:166)
at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:411)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:272)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at example.hello.Client.main(Client.java:52)
Server
package example.hello;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
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(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();
}
}
}
Client
package example.hello;
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();
}
}
}
Interface
package example.hello;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
You need to launch the [RMI] registry. Method getRegistry() does not launch the registry. Method createRegistry(int) launches the registry. You need to change this line in your class Server
Registry registry = LocateRegistry.getRegistry();
to this
Registry registry = LocateRegistry.createRegistry(1099);
Note that 1099 is the default port used by RMI.

java sockets address already in use exception

I'm facing a strange problem doing java sockets project. Here's my code:
Server:
package second.sockets;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server implements Runnable {
public static String HOST = "localhost";
public static int PORT = 1234;
private static final int MAX_USERS = 2;
private static final Server serverInstance = new Server();
private ServerSocket serverSocket;
private List<User> users = new ArrayList<>();
private Server() {
try {
if( this.serverSocket == null ) {
this.serverSocket = new ServerSocket(Server.PORT);
}
} catch(IOException e) {
e.printStackTrace();
System.err.println("could not initialize ServerSocket on port="+Server.PORT +
"["+ e.getMessage() +"]");
}
}
public static Server getInstance() {
return Server.serverInstance;
}
#Override
public void run() {
System.out.println("waiting for incoming connections...");
try {
while( !Thread.interrupted() ) {
this.waitForFreeSlots();
Socket newSocket=null;
try {
newSocket = this.serverSocket.accept();
System.out.println("new connection " + newSocket);
} catch(IOException e) {
System.err.println("could not connect");
}
}
} catch(InterruptedException e) {
System.err.println("server interrupted");
}
}
private synchronized void waitForFreeSlots() throws InterruptedException {
while( this.users.size() >= Server.MAX_USERS ) {
this.wait();
}
}
public static int getPORT() {
return PORT;
}
public static void main(String[] args) {
Server server = Server.getInstance();
Thread serverThread = new Thread(server);
serverThread.start();
}
}
Client:
package second.sockets;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
Socket socket=null;
try {
socket = new Socket(Server.HOST, Server.PORT);
} catch(UnknownHostException uhe) {
System.err.println("could not connect to "+Server.HOST+" on port "+Server.PORT+", no such host");
} catch(IOException ioe) {
System.err.println("could not connect to "+Server.HOST + " on port "+Server.PORT);
}
System.out.println(socket);
}
}
I run the server and it works, but when I run a Client I get could not initialize ServerSocket on port=1234[Address already in use: JVM_Bind] and it is from Server code from line 25. It's odd as Server is a singleton so there's no place for more than one instance of the class. What is more I don't even touch it in Client main function. I don't get it, any ideas where the problem is? Thanks.
EDIT:
Here's the stack trace. It comes from Server's constructor although it is private and it shows after running the Client.
java.net.BindException: Address already in use: JVM_Bind
at java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.net.DualStackPlainSocketImpl.socketBind(DualStackPlainSocketImpl.java:106)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:382)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:190)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.<init>(ServerSocket.java:237)
at java.net.ServerSocket.<init>(ServerSocket.java:128)
at second.sockets.Server.<init>(Server.java:22)
at second.sockets.Server.<clinit>(Server.java:15)
at second.sockets.Client.main(Client.java:12)
could not initialize ServerSocket on port=21234[Address already in use: JVM_Bind]
Socket[addr=localhost/127.0.0.1,port=21234,localport=53054]
I don't get this transition:
at second.sockets.Client.main(Client.java:12)->at second.sockets.Server.<clinit>(Server.java:15)
The stuff you see in the exception comes from the fact that you are using Server.HOST and Server.PORT in your client code. This calls the class initializer for the Server class. In this initializer all the static stuff in the Server class is created. And I see you have a static Server instance there...so you have in fact several Server instances created.
So avoid using Server.HOST and Server.PORT in the client code and use the real valus directly and it will work.
Normally when you want to share stuff between 2 code entities, move them to a third entity to decouple the server and client. Then let server and client use that new entity.
Okay, I figured out what was wrong. In Client in this line socket = new Socket(Server.HOST, Server.PORT); I was using 2 variables from class Server. When I deleted it and pasted raw values or cahnge the variables to be final, it started to work. This is some wild magic.

java RMI over the internet. host refuse connection

we are trying to get RMI working over the Internet. What we tried:
Port forwarding (1099-1100) on Client and Serverside.
shut down firewall in windows and router
tried it with tunngle (www.tunngle.net/)
our RMI Interface:
import java.rmi.RemoteException;
public interface RMIInterface extends java.rmi.Remote {
public void helloWorld(int i) throws RemoteException;
}
our RMI Server Implementation:
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class RMIServerTest extends UnicastRemoteObject implements RMIInterface {
public RMIServerTest() throws RemoteException {
}
#Override
public void helloWorld(int i) throws RemoteException {
for (int j = 0; j < i; j++) {
System.out.println("Hello World");
}
}
public static void main(String[] args) {
try {
LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
}
catch (RemoteException ex) {
System.out.println(ex.getMessage());
}
try {
Naming.rebind("Server", new RMIServerTest());
} catch (MalformedURLException ex) {
System.out.println(ex.getMessage());
} catch (RemoteException ex) {
System.out.println(ex.getMessage());
}
}
}
and our Client:
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
public class RMIClient {
public static void main(String[] args) throws RemoteException, NotBoundException,MalformedURLException {
try {
RMIInterface serverObject = (RMIInterface) Naming.lookup("//externalServerAdress/Server");
serverObject.helloWorld(10);
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
We're still getting this Error:
Connection refused to host: 192.168.0.13; nested exception is:
java.net.ConnectException: Connection timed out: connect
192.168.0.13 is the local IP-adress of the Server behind his router. We connect on client with the external IP of the router. like "2.246.133.155" = externalServerAdress.
So we have a connection. We connect over the external IP adress of the server (WAN IP) and error shows, it gets the local IP-adress of the server, but still refuse connection.
thx for any hint.
Connection refused to host: 192.168.0.13
That's not an Internet address. It is a private address that only exists behind your router. You need to use your public IP address, and arrange port forwarding via your router.

Smack throws "java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserFactory:"

So im using Smack to run my chat bot for league of legends, however I can't even get the bot to show up because of a missing class error that I can't seem to figure out. Code and error below, Thanks for any help, -Nick
Also: yes, this code was taken from an example because when I tried it myself I still got the same error.
package com.nickparks.bot;
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
public class JabberSmackAPI implements MessageListener{
XMPPConnection connection;
public void login(String userName, String password) throws XMPPException
{
ConnectionConfiguration config = new ConnectionConfiguration("chat.na1.lol.riotgames.com",5223);
connection = new XMPPTCPConnection(config);
try {
connection.connect();
connection.login(userName, password, "xiff");
} catch (SmackException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void displayBuddyList()
{
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for(RosterEntry r:entries)
{
System.out.println(r.getUser());
}
}
public void disconnect()
{
try {
connection.disconnect();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
}
public void processMessage(Chat chat, Message message)
{
if(message.getType() == Message.Type.chat)
System.out.println(chat.getParticipant() + " says: " + message.getBody());
}
public static void main(String args[]) throws XMPPException, IOException
{
// declare variables
JabberSmackAPI c = new JabberSmackAPI();
// Enter your login information here
c.login("bot", "Password");
c.displayBuddyList();
System.out.println("-----");
System.out.println("Who do you want to talk to? - Type contacts full email address:");
String talkTo = br.readLine();
System.out.println("-----");
System.out.println("All messages will be sent to " + talkTo);
System.out.println("Enter your message in the console:");
System.out.println("-----\n");
while( !(msg=br.readLine()).equals("bye"))
{
System.out.println("test");
}
c.disconnect();
System.exit(0);
}
}
And here's the error I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserFactory at org.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:321)
atorg.jivesoftware.smack.SmackConfiguration.processConfigFile(SmackConfiguration.java:316)
at org.jivesoftware.smack.SmackConfiguration.<clinit>(SmackConfiguration.java:148)
at org.jivesoftware.smack.ConnectionConfiguration.<init>(ConnectionConfiguration.java:65)
at com.nickparks.bot.JabberSmackAPI.login(JabberSmackAPI.java:16)
at com.nickparks.bot.JabberSmackAPI.main(JabberSmackAPI.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 11 more
You need to have XPP3 (XML Pull Parser 3) in your classpath. Smack 4 does no longer bundle it (unlike Smack 3).
I also recommend using a build system with dependency resolution like maven or gradle, which would automatically fetch the required dependencies.

Categories

Resources