0
votes

I am setting up a chat service using ejabberd and building an XMPP client for android devices using smack.

Here are some important details.

server OS: ubuntu 18.04 server hosted as localhost (jid format: alice@localhost). server system IP : 192.168.4.162

Client: Smack 4.3.1 Using external phone through USB debugging : Nokia 3.1 Plus.

Here is my code

Here are some of the configurations I tried.

  private class MyLoginTask extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            // Create a connection to the jabber.org server.
            InetAddress addr = null;
            try{
                addr = InetAddress.getByName("192.168.4.162");

            }catch(UnknownHostException e){
                e.printStackTrace();
            }
            XMPPTCPConnectionConfiguration config = null;
            DomainBareJid serviceName = null;

            try{
                serviceName = JidCreate.domainBareFrom("localhost");
                System.out.println("serviceName: "+serviceName);
            }catch(XmppStringprepException e){
                e.printStackTrace();
            }

            HostnameVerifier verifier = new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };

//            ConnectionConfiguration connConfig = new ConnectionConfiguration("talk.google.com", PORT, SERVICE);
            try{
                config = XMPPTCPConnectionConfiguration.builder()
                        .setUsernameAndPassword("alice", "9009")
                        .setHost("192.168.4.162")                        
         .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                        .setXmppDomain("localhost")
                        .setHostnameVerifier(verifier)
                        .setHostAddress(addr)
                        .setPort(5222)
                        .build();
            }catch(XmppStringprepException e){
                e.printStackTrace();
            }

            AbstractXMPPConnection conn1 = new XMPPTCPConnection(config);
            try {
                System.out.println("Connecting.......");                AndroidUsingLinkProperties.setup(getApplicationContext());
                conn1.connect().login();
                if(conn1.isConnected()) {
                    Log.w("app", "conn done");
                }
                conn1.login();

                if(conn1.isAuthenticated()) {
                    Log.w("app", "Auth done");
                }
            }
            catch (Exception e) {
                Log.w("app", e.toString());
            }

            return "";
        }


        @Override
        protected void onPostExecute(String result) {
        }

     }

The configurations i tried above the result is:

org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: '192.168.4.162:5222' failed because: /192.168.4.162 exception: java.net.ConnectException: failed to connect to /192.168.4.162 (port 5222) from /192.168.4.182 (port 39568) after 30000ms: isConnected failed: EHOSTUNREACH (No route to host)

however I am successfully able to build a connection to my server through any other client(psi, gajim, My web app(BOSH connection)).

following may help if networking is the issue:

$ nmap localhost
Starting Nmap 7.80 ( https://nmap.org ) at 2019-08-28 18:00 IST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000056s latency).
Not shown: 992 closed ports
PORT     STATE SERVICE
111/tcp  open  rpcbind
139/tcp  open  netbios-ssn
445/tcp  open  microsoft-ds
631/tcp  open  ipp
5222/tcp open  xmpp-client
5269/tcp open  xmpp-server
5280/tcp open  xmpp-bosh
8600/tcp open  asterix

Nmap done: 1 IP address (1 host up) scanned in 0.09 seconds
1

1 Answers

0
votes

I have been there and I found out the accidentally my phone and my ejabberd server were on different network. They should be on the same network as in this case. Make sure you are on same network and this error should go away.