views:

256

answers:

1

Hi I am new in Java. And its giving me a lot of stress. I need to chat with smack api and openfire server. For this my java code is below

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 RunJabberSmackAPI implements MessageListener{

    XMPPConnection connection;

    public void login(String userName, String password) throws XMPPException {
        ConnectionConfiguration config = new ConnectionConfiguration("127.0.0.1 ",5222,"localhost");
        connection = new XMPPConnection(config);

        connection.connect();
        connection.login(userName, password);
    }

    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<RosterEntry> 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
      RunJabberSmackAPI c = new RunJabberSmackAPI();
      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("admin", "admin");    // I created this user with openfire.

      c.displayBuddyList();

      System.out.println("-----");
      System.out.println("Who do you want to talk to? - Type contacts full email address:");
      String talkTo = br.readLine();

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

I run this code twice in my pc. Each for an individual user. I added these two users as friends in openfire by adding rooster. But when they logged in by running the java code above they send there presence as available . But they can't send their presence to each other available. Instead they receives two error messages from their buddy .

First error message :  www.freeimagehosting.net/image.php?eac15f606a.jpg
Second error message : www.freeimagehosting.net/image.php?b827058d07.jpg

I don't know what is wrong with my code. And i really need to solve this problem very soon. I posted this problem other forums too but can't find any answer. So if anyone can have any solution it would be a very big help. Thank You.

A: 

Problem solved. If any one have similar problems than mail me at [email protected]

kalkin