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 + [/192.168.1.186:5222] (/192.168.1.186:5222)' failed because: java.net.SocketTimeoutException: failed to connect to /192.168.1.186 (port 5222) from /192.168.1.197 (port 43530) after 30000ms.
Here’s the relevant code snippet
String serverAddress = "192.168.1.186"; // 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()
.setXmppDomain("ismail.org")
.setHost(serverAddress)
.setPort(serverPort)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) // You can adjust security settings as needed
.build();
AbstractXMPPConnection connection = new XMPPTCPConnection(config);
try {
System.out.println("Trying to connect ....");
connection.connect();
System.out.println("Connected ...");
try {
connection.login("john","john");
} catch (XMPPException | SmackException | IOException e) {
e.printStackTrace();
}
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) {
e.printStackTrace();
}
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?