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 in my local machine ). 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.
String serverAddress = "192.168.1.186"; // Replace with your server's IP or hostname
int serverPort = 5222; // Default XMPP port
String username = "[email protected]";
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?
Edited: i just found a solution to my problem but i don't know if it is the right one but it works , what i did is i found out that my firewall blocked my android device to ping to my server, so i have created a rule to allow the traffic coming throw the port 5222, here are the steps to do so Here are the steps to create an inbound rule for port 5222:
Open the Windows Firewall settings on your Windows 11 laptop.
In the left panel, click on "Inbound Rules."
In the right panel, click on "New Rule..." to open the New Inbound Rule Wizard.
Select "Port" and click "Next."
Choose "TCP" and specify port number 5222. Click "Next."
Select "Allow the connection" and click "Next."
Leave all the profiles (Domain, Private, Public) checked if you want to allow the connection on all types of networks. Click "Next."
Give your rule a name (e.g., "Allow XMPP Port 5222") and, if desired, add a description. Click "Finish" to create the rule.
Test the connection from your Android app to the XMPP server on your Windows laptop. It should now work without having to disable the entire firewall.
This issue was duplicated on the community forums of Smack. The issue was caused (and was fixed) by removing a firewall blockage of the ports that are used by the client (TCP 5222).
More details on https://discourse.igniterealtime.org/t/unable-to-connect-to-xmpp-server-using-smack-on-real-device/