Search code examples
javaandroidc++ice

Error: Ice.ConnectionRefusedException error=0. When I ran as Android application


I have written a Android application in ICE. But when I ran the application, the "LogCat" print the message "Ice.ConnectionRefusedException error=0", I searched a long time, but have no answer. Please, help me! thank you very much. This is two code section. (I'm sorry for my poor English)

Server (C++)

int status = 0;
Ice::CommunicatorPtr ic;
try
{
    ic = Ice::initialize(argc, argv);
    Ice::ObjectAdapterPtr adapter =
        ic->createObjectAdapterWithEndpoints("HelloAdapter", "tcp -p 10000"); 
    Ice::ObjectPtr object = new HelloI();
    adapter->add(object, ic->stringToIdentity("hello"));
    adapter->activate();
    ic->waitForShutdown();
}

Client(Java)

    public void sethelloPrx(){
    Ice.Communicator ic=null;
    try{
        ic=Ice.Util.initialize();
        Ice.ObjectPrx base=
                ic.stringToProxy("hello:tcp -h 127.0.0.1 -p 10000");
        index = helloPrxHelper.checkedCast(base);
        if(index == null)
            throw new Error("Invalid proxy");
    }catch(Exception e){
        Toast.makeText(MainActivity.this,
                "IceMain 注册失败",
                Toast.LENGTH_SHORT).show();
        Log.e("bmi",e.toString());          
    }       
}

Solution

  • In your client you have been written the IP of the server on the proxy like if you could access to the localhost of the server. You have to change the localhost ip on the proxy and put the server's IP. Just modify this line:

     Ice.ObjectPrx base = ic.stringToProxy("hello:tcp -h 127.0.0.1 -p 10000");
    

    to:

    Ice.ObjectPrx base = ic.stringToProxy("hello:tcp -h <server's IP> -p 10000");
    

    You can print the server's IP like this:

    ObjectPrx proxy = adapter->add(object, ic->stringToIdentity("hello"));
    std::cout << proxy << std::endl;
    

    This way you can see the server's IP.

    Little explanation:

    Your client is trying to locate the server on the localhost. If I understood your explanation properly, the client is either on a mobile phone or on an emulator. Since It can't find the server I presume that is on a PC.

    Hope this help you. Edit: this repo is full of ZeroC Ice examples: https://bitbucket.org/arco_group/ice-hello