powered by Jive Software

Recieving messages - code doesn''t work

This is my attempt at developing a simple example for recieving message for a particular user. I seem to have failed miserably Help me!

import org.jivesoftware.smack.filter.*;

import org.jivesoftware.smack.*;

import org.jivesoftware.smackx.*;

import org.jivesoftware.smack.packet.*;

class test{

public test()

{

try{

XMPPConnection con = new XMPPConnection(“tsscc-197”, 5222);

con.login(“pushpavalli”,“pushpavalli”);

}

catch(Exception e){}

PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class), new ToContainsFilter(“pushpavalli@tsscc-197”));

PacketListener list = new PacketListener() {

public void processPacket(Packet packet) {

Message message = (Message)packet;

System.out.println(message.getFrom());

System.out.println(message.getBody());

}};

con.addPacketListener(list, filter);

}catch(Exception e){

System.out.println(e);}

}

public static void main(String args[])

{

test tst = new test();

}

}

My code shown above is my attempt to get all the messages to the user “pushpavalli” from other users and print them. But, it does not give any output. It does not print anything. Neither does it throw and exception.

I ran this program and I tried messaging the program user from another user in exodus. But, nothing seems to be given out.

Hey Vimal,

Have you used the Smack debugger before? The debugger will let you see the sent and received packets from the server. This information could be very valuable when debugging problems.

Since the server will only send packets to a user that were meant for him there is no need use the filter ToContainsFilter. I see that your sample code is leaving immediately so I guess that it does not have a chance to receive any message. Try adding a while loop or delay after the test instance was created.

Regards,

– Gato

Dear Mr.Gato,

Thank you for your suggestion. It worked. I created a GUI with a text area to recieve the messages and tested out the program. As you have said, the delay required was not present.

I had been wondering if it was my packet listener that was the problem!

Thanks and regards,

Vimal Kumar S