0

I try to launch this app using RMI client-server. Firstly, I ran it and had the error "Connection refused to host: localhost". After that I went go system32/drivers/etc/hosts and fix it, added line:

127.0.0.1 localhost

It wasn't led me to problem solution. Then I looked up same questions in stackoverflow about how to fix this problem, then solved it with (ran in cmd):

start rmiregistry

So, rmiregistry ran and i had got a new error - NotBoundException (but I could fix "Connection refusal" problem).

servicebrowser.java:

package servicebrowser;

import java.awt.*;
import javax.swing.*;
import java.rmi.*;
import java.awt.event.*;


public class ServiceBrowser {


    JPanel mainPanel;
    JComboBox serviceList;
    ServiceServer server;

    public void buildGUI() {


        Object[] services = getServicesList();


    }


    Object[] getServicesList() {
        Object obj = null;
        Object[] services = null;

        try {

            obj = Naming.lookup("rmi://127.0.0.1/ServiceServer");

        }
        catch (Exception ex) { ex.printStackTrace(); }

        server = (ServiceServer) obj;

        try {
            services = server.getServiceList();
        }
        catch (Exception ex) { ex.printStackTrace(); }
        return services;        
    }

    class MyListListener implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
            Object selection = serviceList.getSelectedItem();
            loadService(selection);           
        }        

    }



    public static void main(String[] args) {

       new ServiceBrowser().buildGUI(); 


    }

}

class ServiceServerImpl:

import java.rmi.*;
import java.util.*;
import java.rmi.server.*;


public class ServiceServerImpl extends UnicastRemoteObject
    implements ServiceServer {

    HashMap serviceList;

    public ServiceServerImpl() throws RemoteException {
        setUpServices();
    }

    private void setUpServices() {
        serviceList = new HashMap();

    }

    public Object[] getServiceList() {
        System.out.println("in remote");
        return serviceList.keySet().toArray();       
    }

    public Service getService(Object serviceKey) throws RemoteException {
        Service theService = (Service) serviceList.get(serviceKey);
        return theService;
    }


    public static void main (String[] args) {

        try {

            Naming.rebind("ServiceServer", new ServiceServerImpl());   

        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println("Remote service is running");
    }

}

What is wrong with it? I turned off firewall too, certaintly.

msn013
  • 103
  • 9
  • 1
    Do you have a stacktrace you can show us? What line is producing this error? Your mixing in discussion of other errors you're (still?) getting is confusing. – CryptoFool Apr 03 '19 at 00:54
  • Oh, no. I fixed latest errors. Now I have only one: "NotBoundException". – msn013 Apr 03 '19 at 00:59
  • Unless your server and client and service-browser are all running on the same machine, `Naming.lookup("rmi://127.0.0.1/ServiceServer")` is looking on the wrong host. You need to provide the hostname or IP address of the server host. Which also means you don't need to run the `rmiregistry` in the client host. – user207421 Apr 03 '19 at 04:26
  • If i don't run the rmiregistry in my local host, this code throws exception with "Connection refused to host: localhost" – msn013 Apr 03 '19 at 13:52
  • You need to run it in the server host, and not in the client host. – user207421 Apr 03 '19 at 16:32

1 Answers1

0

Thanks a lot!

I solved my problem right this way.

Firstly, I edited classes servicebrowser, ServiceServerImpl.

class servicebrowser:

Before:

 try {

     obj = Naming.lookup("rmi://127.0.0.1/ServiceServer");

 }

Now (plus I added import java.rmi.registry.LocateRegistry, import java.rmi.registry.Registry in top part of code):

try {
    Registry registry = LocateRegistry.getRegistry("127.0.0.1", 10001);
    obj = registry.lookup("ServiceServer");

}

class ServiceServerImpl:

Before:

try {

    Naming.rebind("ServiceServer", new ServiceServerImpl());   

}

Now (like a previous class I added import classes in top part of code):

try {
    Registry registry = LocateRegistry.createRegistry(10001);
    registry.bind("ServiceServer", new ServiceServerImpl());   

}

Secondly, I try to ran project (F6) in Netbeans, where servicebrowser marked as main class. It was refusal connection again. After that I only ran class ServiceServerImpl (Shift + F6) then ran entire projecе. So, it works.

P.S. I didn't use cmd and try to

"start rmiregistry"

because the app works without it.

msn013
  • 103
  • 9
  • The first lot of changes made no difference whatsoever. The code is fully equivalent except that you're using a different port. – user207421 Apr 03 '19 at 23:35
  • Mm... It may be true, but if I use another port in initial app version (for example, 10001), like this: obj = Naming.lookup("rmi://127.0.0.1:10001/ServiceServer"); and try to run class, then run entire project, it led to "connection refusal".What's wrong with it? – msn013 Apr 04 '19 at 00:26