powered by Jive Software

Building a simple client with smack(so close....yet so far...)

I just started programming jabber clients yesterday (and java for about a week now). The code below works, but only for about 9 seconds or so. I can connect to my jabber server and even send messages back and forth, but it will shut down after a while…

import java.util.*;

import java.io.*;

import org.jivesoftware.smack.Chat;

import org.jivesoftware.smack.ConnectionConfiguration;

import org.jivesoftware.smack.MessageListener;

import org.jivesoftware.smack.Roster;

import org.jivesoftware.smack.RosterEntry;

import org.jivesoftware.smack.XMPPConnection;

import org.jivesoftware.smack.XMPPException;

import org.jivesoftware.smack.packet.Message;

public class JabberSmackAPI implements MessageListener{

** XMPPConnection connection;**

** public void login(String userName, String userName) throws XMPPException**

** {**

** ConnectionConfiguration config = new ConnectionConfiguration(“mydomain.com”,5222);**

** connection = new XMPPConnection(config);**

** connection.connect();**

** connection.login(userName, userName);**

** }**

** public void sendMessage(String message, String to) throws XMPPException**

** {**

** Chat chat = connection.getChatManager().createChat(to, this);**

** chat.sendMessage(message);**

** }**

** public void displayBuddyList()**

** {**

** Roster roster = connection.getRoster();**

** Collection entries = roster.getEntries();**

** System.out.println("\n\n" + entries.size() + " buddy(ies):");**

** for(RosterEntry r:entries)**

** {**

** System.out.println(r.getUser());**

** }**

** }**

** public void disconnect()**

** {**

** connection.disconnect();**

** }**

** public void processMessage(Chat chat, Message message)**

** {**

** if(message.getType() == Message.Type.chat)**

** System.out.println(chat.getParticipant() + " says: " + message.getBody());**

** }**

** public static void main(String args[]) throws XMPPException, IOException**

** {**

** // declare variables**

** JabberSmackAPI c = new JabberSmackAPI();**

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

** String msg;**

** // turn on the enhanced debugger**

** //XMPPConnection.DEBUG_ENABLED = true;**

** // Enter your login information here**

** c.login(“myusername”, “mypassword”);**

** c.displayBuddyList();**

** System.out.println("-----");**

** **** String talkTo =("admin@mydomain.com");**

** System.out.println("-----");**

** System.out.println("All messages will be sent to " + talkTo);**

** System.out.println(“Enter your message in the console:”);**

** System.out.println("-----\n");**

** while( !(msg=br.readLine()).equals(“bye”))**

** {**

** c.sendMessage(msg, talkTo);**

** }**

** c.disconnect();**

** System.exit(0);**

** }**

}

Error code:

**
**

stream:error (text)

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

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

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

Is there a way for me to keep the client running longer? Forever would be nice!

OK, I got it working. I was logged into the same user account on two different eclipse IDEs. So I restarted the jabber server and logged into only one computer. Working perfectly now.

Hey can u make me a simple one for facebook please…