2012-03-19 87 views
1

我想连接到使用smack API的openfire服务器,我无法这样做。无法连接到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("[email protected]", "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 

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

回答

2
connection.login("[email protected]", "setup1"); 

如果您的服务器在本地主机上启动,则绝对不应该登录到example.com域。 尽量只:

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

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

+0

k ...这工作。所以如果服务器是在实际域中,那么我应该使用[email protected]从客户端设备登录,对吧? – frewper 2012-03-19 10:16:36

+0

我认为你总是可以用用户名登录,但[email protected]也应该可以工作。 – Maggie 2012-03-19 10:23:38

+0

在XMPP界面上,您总是需要一个域。我不知道这个API是否需要它,但我会虚心地暗示,为了一致性,无论如何都使用域是个好主意。 – 2012-03-19 10:51:43