Why automatic disconnection after message sended

Hi all,

I’m new in this community, I’m tryin to let one of examples work.

I already have Openfire configured and perfectly working, when I try to run this example below I receive the message on my second user (on pidgin client) and the sender automatic disconnect itself with no reason, I cannot send messages from my second user to the client running on eclipse (the example).

That’s the example:

import org.jivesoftware.smack.*;

import org.jivesoftware.smack.packet.*;

public class Connect2 {

public static void main(String [] args) {

System.out.println(“start”);

ConnectionConfiguration config = new ConnectionConfiguration(“MYSERVER”, 5222);

XMPPConnection con = new XMPPConnection(config);

try {

con.connect();

} catch(XMPPException e) {

System.out.println(“error connection”);

System.exit(1);

}

System.out.println(“connection ok”);

try {

con.login(“user1”, “password”);

} catch(XMPPException e) {

System.out.println("error login "+e.getMessage());

System.exit(1);

}

System.out.println(“login ok”);

try {

Chat chat = con.getChatManager().createChat(“pidginuser@server”, new MessageListener() {

public void processMessage(Chat chat, Message message) {

System.out.println("Received message: " + message);

}

});

chat.sendMessage(“I am bot!”);

} catch(XMPPException e) {

System.out.println(“error chat”);

System.exit(1);

}

}

}

Can anyone help me? Why my client on eclipse close itself and don’t let me receive messages from my pidgin client?

Thanks all!!

What do you think is going to happen when your main method finishes? After you do a chat.sendMessage(), your program is going to end since you have reached the end of the main method.

Yep, my question was not properly posted.

I’ve now setup a client that keep connection up, but what I need is that the client, once connected to the jabber server, will be able to receive every message from anyone.
I’ve found this example:

// Assume we’ve created a Connection name “connection”.

ChatManager chatmanager = connection.getChatManager().addChatListener(

new ChatManagerListener() {

@Override

public void chatCreated(Chat chat, boolean createdLocally)

{

if (!createdLocally)

chat.addMessageListener(new MyNewMessageListener());;

}

});

When I try to implement it gives me an error because “getChatManager()” is a Chat object method, not a Connection method.

Can anyone post me a working example of a client that receive messages from anyone with imports and calls?

Thanks a lot

Your problem is that addChatListener is a void method, and you are trying to assign the variable ChatManager as its return type.

Other than that you are on the right track.