Unable to Connect to XMPP Server using Smack on Real Device

I’m working on an Android app that uses the Smack library (version 4.4.6) to connect to an XMPP server (i am using Openfire). The code works perfectly fine in the emulator, but when I try to run it on a real device, I encounter the following error:

org.jivesoftware.smack.SmackException$EndpointConnectionException: The following addresses failed: 'rfc6120 a/aaaa endpoint + [/] (/' failed because: java.net.SocketTimeoutException: failed to connect to / (port 5222) from / (port 43530) after 30000ms.

Here’s the relevant code snippet

String serverAddress = ""; // Replace with your server's IP or hostname
int serverPort = 5222; // Default XMPP port
String username = "john@gmail.com";
String password = "john";

XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
        .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) // You can adjust security settings as needed

AbstractXMPPConnection connection = new XMPPTCPConnection(config);

try {
    System.out.println("Trying to connect ....");
    System.out.println("Connected ...");
    try {
    } catch (XMPPException | SmackException | IOException e) {
    System.out.println("Logged in");

    System.out.println("Start searching .....");
    boolean alpha = exist("alpha", connection);
    System.out.println("The user exists or not: " + alpha);

} catch (Exception e) {

As mentioned, this code works fine in the emulator, but not on a real device. Can anyone help me understand why I’m encountering this error on a real device and how I can resolve it?

The primary issue is not a coding issue, it is an infrastructure issue. There is some kind of networking configuration that prevents your real device to reach the server that is running the service for the XMPP domain name ismail.org on a host that is expected to be reachable on IP address

As an aside: In real-world scenario’s, you ideally do not need to provide the server address or IP address. Using DNS SRV lookups, Smack should be able to find the correct IP address for the server that is providing the XMPP service for the domain (ismail.org) that you’re attempting to use. This will require correct configuration of DNS records. If that is not feasible, then you can ‘override’ this lookup, by supplying a hostname or IP address, like this code snippet is doing.

This code also seems to show a misunderstanding on how XMPP user identifiers (JIDs) are construted. You have configured your XMPP domain to be ismail.org but your username includes a reference to the domain gmail.com. Users on your Openfire domain (which I assume you have configured to use the XMPP domain ismail.org) will have something like this as their JID: john@ismail.org, not john@gmail.com. It should actually not be needed at all to include the domain in the JID when you set the XMPP domain in the connection configuration. Simply using the username john should be enough.

Thanks, @guus, yes you were right, it was a networking issue, I found out that my firewall is blocking the incoming traffic throw port 5222. So I have added a rule to allow this port and it works just fine.


This topic was automatically closed 62 days after the last reply. New replies are no longer allowed.