XMPP Java客户端 - 未连接到服务器

时间:2014-04-18 08:33:22

标签: java eclipse client xmpp smack

我的XMPP Java客户端出现问题。

我有一个XMPP服务器(基于Ejabberd,10.30.16.147)和两个Pidgin用户:test1 @ localhost(在我的Windows机器上,10.30.16.199)和test2 @ locahost(在Linux服务器机器上,10.30.16.147)

当我使用两个Pidgin客户端时,聊天工作正常。 现在我正在尝试创建一个Java客户端,只是为了模拟两个Pidgin客户端中的一个。代码如下(我在Eclipse项目中导入了Smack 3.4.1库):

package test2XMPP;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.MessageListener;

public class Sender {

    public static void main(String a[]) throws XMPPException, InterruptedException {
        ConnectionConfiguration config = new ConnectionConfiguration("10.30.16.147", 5222);
        XMPPConnection connection = new XMPPConnection(config);
        connection.connect();
        connection.login("test1@locahost", "test1");

        Chat chat = connection.getChatManager().createChat("test2@localhost", new MessageListener() {

            public void processMessage(Chat chat, Message message) {
                System.out.println("Received message: " + message);
            }
        });

        chat.sendMessage("Howdy test1!");

        while (true) {
            Thread.sleep(50);
        }
    }
}

我得到以下控制台输出:

    apr 18, 2014 10:19:37 AM org.jivesoftware.smack.provider.UrlProviderFileInitializer initialize
    INFO: Loading providers for file [classpath:META-INF/core.providers]
    apr 18, 2014 10:19:37 AM org.jivesoftware.smack.provider.UrlProviderFileInitializer initialize
    INFO: Loading providers for file [classpath:META-INF/extension.providers]
    Exception in thread "main" java.lang.IllegalStateException: Not connected to server.
        at org.jivesoftware.smack.XMPPConnection.login(XMPPConnection.java:230)
        at org.jivesoftware.smack.Connection.login(Connection.java:368)
        at test2XMPP.Sender.main(Sender.java:18)

如果我已经与我的Pidgin test1 @ localhost客户端断开连接,我也会收到这些错误。

你有什么建议吗? 感谢

2 个答案:

答案 0 :(得分:1)

使用new ConnectionConfiguration("10.30.16.147", 5222, "localhost");connection.login("test1", "test1");

答案 1 :(得分:0)

有类似的经历,问题是主机名&域名不同。 我正在使用ConnectionConfiguration类的单个参数构造函数。

ConnectionConfiguration config = new ConnectionConfiguration(hostname);

只需要识别安装了Ejabberd的机器的主机名&确保两者都是相同的,以便客户进行沟通。

相关问题