javaxmppopenfiresmack

Openfire does not respond to Smack login()


I am using Smack API trying to connect and login to the Openfire XMPP server which is setup on my laptop (localhost).

    XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
            .setServiceName("win10-xps15")
            .setHost("192.168.1.100")
            .setPort(5222)
            .setCompressionEnabled(false)
            .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
            .build();

    XMPPTCPConnection connection = new XMPPTCPConnection(conf);

    connection.connect();
    connection.login("admin", "password");

The connect() method is successful but the login() method fails with a NoResponse (timeout) exception.

Exception in thread "main" org.jivesoftware.smack.SmackException$NoResponseException: No response received within reply timeout. Timeout was 5000ms (~5s). Used filter: No filter used or filter was 'null'.
    at org.jivesoftware.smack.SmackException$NoResponseException.newWith(SmackException.java:106)
    at org.jivesoftware.smack.SmackException$NoResponseException.newWith(SmackException.java:85)
    at org.jivesoftware.smack.SASLAuthentication.authenticate(SASLAuthentication.java:250)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection.loginNonAnonymously(XMPPTCPConnection.java:374)
    at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:456)
    at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:431)
    at MyConnector.main(MyConnector.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Apr 13, 2016 11:45:42 AM org.jivesoftware.smack.AbstractXMPPConnection callConnectionClosedOnErrorListener
WARNING: Connection closed with error
java.lang.NullPointerException
    at org.jivesoftware.smack.util.stringencoder.Base64.decode(Base64.java:86)
    at org.jivesoftware.smack.sasl.SASLMechanism.challengeReceived(SASLMechanism.java:233)
    at org.jivesoftware.smack.SASLAuthentication.challengeReceived(SASLAuthentication.java:328)
    at org.jivesoftware.smack.SASLAuthentication.challengeReceived(SASLAuthentication.java:313)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.parsePackets(XMPPTCPConnection.java:1051)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader.access$300(XMPPTCPConnection.java:948)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection$PacketReader$1.run(XMPPTCPConnection.java:963)
    at java.lang.Thread.run(Thread.java:745) 

I am running Openfire 3.9.3 on my Windows laptop and the local IP is 192.168.1.100. The version of the smack library is 4.1.6.

Since it is the login() method that fails, I wonder if there is any Openfire server settings related to login/user that I should be aware of?

In addition, I tried to login with the Spark IM client and it could login the local server without problems.

Regards.


More info added below.

I just tried an old version (3.4.1) of smack obtained from the Spark IM github repo with the following code:

    ConnectionConfiguration conf = 
            new ConnectionConfiguration("192.168.1.100", 5222);
    XMPPConnection connection = new XMPPConnection(conf);

    connection.connect();
    connection.login("admin", "password");

Now the code works fine. Both connect() and login() are OK.

But I really want to know why the lastet version of smack does not work as I expect. Because I am considering using smack on Android while the old version does not support Android natively.


Solution

  • Lets start with step by step

    1) add dependencies

    compile 'org.igniterealtime.smack:smack-tcp:4.1.0'
    compile 'org.igniterealtime.smack:smack-android-extensions:4.1.0'
    compile 'org.igniterealtime.smack:smack-im:4.1.0'
    

    2) async task for connect or login to server

    //connect or login to server
    private class MyOpenfireLoginTask extends AsyncTask<String, String, String> {
        private Context mContext;
        String username, password;
        ProgressDialog dialog;
    
    
        public MyOpenfireLoginTask(Context context) {
            mContext = context;
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(mContext);
            dialog.setMessage("Loading...");
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        }
    
        @Override
        protected String doInBackground(String... params) {
    
            username = params[0];
            password = params[1];
    
            Log.e("Login using ", username + " , " + password);
            Config.config = XMPPTCPConnectionConfiguration.builder()
                    .setUsernameAndPassword(username, password)
                    .setHost("your host ex- wec.com")
                    .setResource("Android")
                    .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                    .setServiceName("domain name(@) ex-   secureserver.net")
                    .setPort("5222")
                    .setDebuggerEnabled(true)
                    .build();
            Config.conn1 = new XMPPTCPConnection(Config.config);
    
            Config.conn1.setPacketReplyTimeout(5000);
            try {
                Config.conn1.connect();
                if (Config.conn1.isConnected()) {
                    Log.w("app", "conn done");
    
                }
                Config.conn1.login();
    
    
                if (Config.conn1.isAuthenticated()) {
                    Log.w("app", "Auth done");
    
                } else {
                    Log.e("User Not Authenticated", "Needs to Update Password");
    
                }
    
            } catch (Exception e) {
                Log.w("app", e.toString());
            }
    
            return "";
        }
    
        @Override
        protected void onPostExecute(String result) {
    
    
            dialog.dismiss();
    
            if (Config.conn1.isAuthenticated()) {
                 //success
    
            } else {
                Log.e(TAG, "username password wrong");
    
            }
        }
    }
    

    3)execute above class

      if (username.length() > 0 && password.length() > 0) {
            MyOpenfireLoginTask task = new MyOpenfireLoginTask(activity);
            task.execute(username, password);
        }