Unable to connect to local openfire XMPP server

3.1k views Asked by At

I am working on an android app, I have been trying to connect to the openfire server installed locally. The server is up and running but when I try to run the app on my phone it does not connect to the server.

Code:

public class ChatService extends Service implements ConnectionListener {
    private static Configuration configuration;
    private static final String TAG = ChatService.class.getSimpleName();
    public static void setupConnection(){
        configuration = new Configuration();
        final ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(configuration.getHost(), configuration.getPort(), configuration.getServiceName());
        Log.d(TAG, "Host: " + configuration.getHost());
        Log.d(TAG, "Port: " + configuration.getPort());
        Log.d(TAG, "Service name: " + configuration.getServiceName());
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                configuration.setDebuggerEnabled(true);
                XMPPConnection connection = new XMPPTCPConnection(connectionConfiguration);

                try {
                    connection.connect();
                    if(connection.isConnected()){
                        Log.d(TAG, "Connected to server");
                    }
                } catch (SmackException.ConnectionException e) {
                    for (int i = 0; i < e.getFailedAddresses().size(); i++) {
                        HostAddress element = e.getFailedAddresses().get(i);
                        Log.e("ERROR", element.getErrorMessage().toString());
                    }
                    e.printStackTrace();
                } catch (SmackException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XMPPException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }

In the logcat I am getting SmackException$ConnectException.

Log:

11-10 00:48:20.400  18449-18860/com.example.puneet.chat E/ERROR﹕ localhost:5222 Exception: failed to connect to localhost/127.0.0.1 (port 5222): connect failed: ECONNREFUSED (Connection refused)
11-10 00:48:20.408  18449-18860/com.example.puneet.chat W/System.err﹕ org.jivesoftware.smack.SmackException$ConnectionException
11-10 00:48:20.408  18449-18860/com.example.puneet.chat W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:433)
11-10 00:48:20.408  18449-18860/com.example.puneet.chat W/System.err﹕ at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:808)
11-10 00:48:20.408  18449-18860/com.example.puneet.chat W/System.err﹕ at org.jivesoftware.smack.XMPPConnection.connect(XMPPConnection.java:396)
11-10 00:48:20.408  18449-18860/com.example.puneet.chat W/System.err﹕ at XMPPConnection.ChatService$1.run(ChatService.java:45)
11-10 00:48:20.408  18449-18860/com.example.puneet.chat W/System.err﹕ at java.lang.Thread.run(Thread.java:856)

I would really appreciate if someone could help me understand why am I getting this exception and what is wrong

2

There are 2 answers

0
Mohsin Patel On

set configuration.host as

http://127.0.0.1:5222

then try

if there have still problem then go to Openfire > server > server settings > client connections and then check the port number which have SSL (generally 5223) and enable SSL and use host with 5223 port number.

http://127.0.0.1:5223
0
mubeen On

When you are connecting emulator from localhost. You donot use 127.0.0.1 ip instead use 10.0.2.2 to connect to localhost server.