Adrian
Adrian

Reputation: 1707

Connecting to a ejabberd XMPP server on localhost using Smack API on android

I have an ejabberd XMPP server installed locally on a mac. I'm using this code to connect and login using Smack API on android.

config = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword("[email protected]", "1")
                .setHost("192.168.1.2")
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setServiceName("192.168.1.2")
                .setPort(Integer.parseInt("5222"))
                .build();

        AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
        try {
            conn2.connect();
            conn2.login();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        }

Using the same username and password, Im able to login using any other XMPP client like Adium but the code above gives this error on android -

Connection closed with error org.jivesoftware.smack.XMPPException$StreamErrorException: host-unknown

My Local address is 192.168.1.2 and the ejabberd admin panel is localhost:5280/admin.

I read the documentation and did everything written. Any problem with the code or something here ?

Upvotes: 0

Views: 4090

Answers (5)

Mahdi Moqadasi
Mahdi Moqadasi

Reputation: 2479

Replace setHost() with setHostAddress(). I don't know why, but Ejabberd on a windows server gives TimeOut for me. If you are using it on windows, test with linux-based OS also.

Upvotes: 0

MD. AL-HASAN MRIDHA
MD. AL-HASAN MRIDHA

Reputation: 1

This code should work for smack 4.2.4

XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
            .setHostAddress(InetAddress.getByName("192.168.1.2"))
            .setUsernameAndPassword("[email protected]", "1")
            .setXmppDomain(JidCreate.domainBareFrom("localhost"))
            .setResource("Rooster")
            .setKeystoreType(null) 
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .setCompressionEnabled(true).build();

Upvotes: 0

Tushar Purohit
Tushar Purohit

Reputation: 534

you should try following :-

  1. try changing your port to 5280 or try 5227
  2. check your server security config if it is required or optional because you have set security as disable
  3. add connection Listener to your xmpp connection
  4. setDebugEnable(true) to check the smack log in your console and add log to your question, that will give more clarity to question about what exactly happening
  5. try changing ConnectionConfiguration.SecurityMode.disabled with XMPPTCPConnectionConfiguration.SecurityMode.required or XMPPTCPConnectionConfiguration.SecurityMode.optional

Upvotes: 0

Adrian
Adrian

Reputation: 1707

I made it working to connect and login to the server this way -

config = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword("1", "1")
                .setHost("192.168.1.2")
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setServiceName("davids-macbook-pro.local")
                .setPort(Integer.parseInt("5222"))
                .build();

        AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
        try {
            conn2.connect();
            conn2.login();
        } catch (SmackException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XMPPException e) {
            e.printStackTrace();
        }

Upvotes: 0

TheKalpit
TheKalpit

Reputation: 1486

  • username should be appended by "@"
  • setDebuggerEnabled(true) to view what's happening behind the scenes
  • move connect() and login() methods to a background thread
  • call login() after connection is established successfully using connection listener

config = XMPPTCPConnectionConfiguration.builder()
                .setUsernameAndPassword("[email protected]", "1")
                .setHost("192.168.1.2")
                .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                .setServiceName("davids-macbook-pro.local")
                .setPort(5222)
                .setDebuggerEnabled(true) // to view what's happening in detail
                .build();

AbstractXMPPConnection conn2 = new XMPPTCPConnection(config);
try {
    conn2.connect(); // move it to a background thread instead of main thread
    // conn2.login();
} catch (SmackException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (XMPPException e) {
    e.printStackTrace();
}


conn2.addConnectionListener(new ConnectionListener() {
    @Override
    public void connected(XMPPConnection connection) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    conn2.login();
                } catch (SmackException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XMPPException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute();
    }

    @Override
    public void authenticated(XMPPConnection connection, boolean resumed) {

    }

    @Override
    public void connectionClosed() {

    }

    @Override
    public void connectionClosedOnError(Exception e) {

    }

    @Override
    public void reconnectionSuccessful() {

    }

    @Override
    public void reconnectingIn(int seconds) {

    }

    @Override
    public void reconnectionFailed(Exception e) {

    }
});

Upvotes: 0

Related Questions