Help on Smack API

Dear All!!

I went through the SMACK APIs and i found it very interesting to work with. but i dont know how to start thte work.

PLease anybody send me a simple one to one chat program that uses WildFire server.

i tried main program but none is working. plz forward me the simple but working program.

also tell me if it is coorect

Chat c1=con.createChat(“demo@localhost”); if i hv to connect to demo client on the local machine.

Help me i am struck

Regards

Bhopu

The documentation and javadocs are really very complete. If you work through them you shouldnt have any trouble in doing what you want.

Basic steps

Create connection to jabber server

Login

Send message

Exit

Post what you have got so far and someone can hopefully point out where you are going wrong

Jon

/*

  • Created on Jan 7, 2007
  • TODO To change the template for this generated file go to

  • Window - Preferences - Java - Code Style - Code Templates

*/

/**

  • @author 501409766
  • TODO To change the template for this generated type comment go to

  • Window - Preferences - Java - Code Style - Code Templates

*/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import org.jivesoftware.smack.*;

import org.jivesoftware.smack.filter.PacketFilter;

import org.jivesoftware.smack.packet.*;

public class User2

{

static XMPPConnection con2=null;

Thread t;

//set the listener thread

class Listener implements Runnable

{

//set thread

public Listener(String name)

{

t=new Thread(this,name);

t.start();

}

public void run()

{

//infinitely wait for the incoming packet

while(true)

{

//define a packet filter to accept all messages

PacketFilter myFilter=new PacketFilter()

{

public boolean accept(Packet p)

{

/*if((p.getFrom().equals(“sumit@localhost”)))

return true;

else

return false;*/

return true;

}

};

/* way 1 -


use of PacketCollector Class

//Collect these messages

PacketCollector collector = con2.createPacketCollector(myFilter);

//add the msg listener

GroupChatListener gcl=new GroupChatListener();

con2.addPacketListener(gcl,myFilter);

//read the packets

Packet packet = collector.nextResult();

if (packet instanceof Message) {

Message msg = (Message) packet;

System.out.println("MSG ID: "+msg.getPacketID());

System.out.println(“Message:”+msg.getBody());

}

*/

// way 2---- use of PacketListener Interface

PacketListener myListener=new PacketListener()

{

public void processPacket(Packet packet)

{

if(packet instanceof Message)

{

Message msg=(Message)packet;

System.out.println("MSG ID: "+msg.getPacketID());

System.out.println("Message: "+msg.getBody());

}

}

};

//register the packet listerner with the connection

con2.addPacketListener(myListener,myFilter);

}//end of while for listener

}//end of listener run method

} //end of Listener class

//set thte sender thread

class Sender implements Runnable

{

//set the thread

public Sender(String Name )

{

t=new Thread(this,Name);

t.start();

}

public void run()

{

//config the sender thead

while(true)

{

String msg=null;

System.out.println(“Ur Msg(bye to exit):”);

try

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

msg=br.readLine();

if(msg.equalsIgnoreCase(“bye”))

{

System.out.println(“Program is closing…”);

System.exit(0);

}

else

{

System.out.println(msg);

//code for sending msg

Message Msg=new Message();

Msg.setBody(msg);

Msg.setTo(“sumit”);

con2.sendPacket(Msg);

}

}

catch(IOException e)

{

e.printStackTrace();

}

}//end of while for sender

}//end of sender run method

}//end of Sender class

public static void main(String[] args)

{

//create connection and login for usr2

try

{

con2=new XMPPConnection(“localhost”);

con2.login(“sumit”,“alok”);

}

catch(XMPPException e)

{

System.out.println(“Connection Error”);

}

catch(Exception e)

{

System.out.println(“Program closed due to unexpected errors. Please restart again…”);

}

//create a chat with user2

//Chat c1=new Chat(con1,“sumit@localhost”);

Chat c1=con2.createChat(“dixit”);

//create new threads for Sender and Listener

User2 usr=new User2();

usr.new Listener(“tListener”);

usr.new Sender(“tSender”);

//wait infinitely

//System.in.read();

}

}

pls smbody check it and tell me wat is the error.

i am getting this error. can can body explain?

stream:error (conflict)

at org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:320)

at org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43)

at org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:63)