无法在真实设备上使用 Smack 连接到 XMPP 服务器

问题描述 投票:0回答:1

我正在开发一个 Android 应用程序,它使用 Smack 库(版本 4.4.6)连接到 XMPP 服务器(我在本地计算机中使用 openfire )。该代码在模拟器中运行得很好,但是当我尝试在真实设备上运行它时,遇到以下错误:

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();
}


如前所述,此代码在模拟器中运行良好,但在真实设备上则不然。谁能帮助我理解为什么我在真实设备上遇到此错误以及如何解决它?

编辑:我刚刚找到了解决我的问题的方法,但我不知道它是否正确,但它有效,我所做的是我发现我的防火墙阻止了我的 Android 设备 ping 到我的服务器,所以我有创建了一条规则以允许流量通过端口 5222,以下是执行此操作的步骤 以下是为端口 5222 创建入站规则的步骤:

在 Windows 11 笔记本电脑上打开 Windows 防火墙设置。

在左侧面板中,单击“入站规则”。

在右侧面板中,单击“新建规则...”以打开新建入站规则向导。

选择“端口”并单击“下一步”。

选择“TCP”并指定端口号5222。单击“下一步”。

选择“允许连接”,然后单击“下一步”。

如果您想允许在所有类型的网络上进行连接,请选中所有配置文件(域、专用、公共)。单击“下一步”。

为您的规则命名(例如“允许 XMPP 端口 5222”),并根据需要添加说明。单击“完成”创建规则。

测试从 Android 应用程序到 Windows 笔记本电脑上的 XMPP 服务器的连接。现在它应该可以工作,而无需禁用整个防火墙。

java android xmpp openfire smack
1个回答
0
投票

此问题在 Smack 社区论坛上重复出现。该问题是通过删除客户端使用的端口 (TCP 5222) 的防火墙阻止引起的(并已修复)。

更多详细信息请参见 https://discourse.igniterealtime.org/t/unable-to-connect-to-xmpp-server-using-smack-on-real-device/

© www.soinside.com 2019 - 2024. All rights reserved.