Search code examples
javaxmppsmack

Can't Connect to Openfire server


I am trying to connect to an openfire server using smack API, I am unable to do so.

Here is the code:

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

The following is the output:

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

It seems to connect to the server but can't log in.


Solution

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

    You definitely shouldn't be logging in to example.com domain if your server is started on localhost. Try just:

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

    But remember that to be able to login, you need to have a valid username and password. That means you have to create user "test" with password "setup1" on your server.