无法连接到Openfire服务器

时间:2012-03-19 10:02:40

标签: java xmpp smack

我正在尝试使用smack API连接到openfire服务器,我无法这样做。

以下是代码:

public class Tests{

public static void main( String[] args ) {

    System.out.println("Starting IM client");

    // gtalk requires this or your messages bounce back as errors
   ConnectionConfiguration connConfig = new ConnectionConfiguration("localhost", 5222);
   XMPPConnection connection = new XMPPConnection(connConfig);

    try {
        connection.connect();
        System.out.println("Connected to " + connection.getHost());
    } catch (XMPPException ex) {
        //ex.printStackTrace();
        System.out.println("Failed to connect to " + connection.getHost());
        System.exit(1);
    }
    try {
        connection.login("test@example.com", "setup1");
        System.out.println("Logged in as " + connection.getUser());

        Presence presence = new Presence(Presence.Type.available);
        connection.sendPacket(presence);

    } catch (XMPPException ex) {
        //ex.printStackTrace();
        System.out.println("Failed to log in as " + connection.getUser());
        System.exit(1);
    }
   connection.disconnect();  
}
}

以下是输出:

Starting IM client
Connected to localhost
Failed to log in as null

似乎连接到服务器但无法登录。

1 个答案:

答案 0 :(得分:2)

connection.login("test@example.com", "setup1");

如果您的服务器是在localhost上启动的,那么您绝对不应该登录example.com域。 试试吧:

 connection.login("test", "setup1");

但请记住,为了能够登录,您需要拥有有效的用户名和密码。这意味着您必须在服务器上创建密码为“setup1”的用户“test”。