title: Sending and receiving message using Java Smack API not working on example
body: <p>I am still trying to learn how to properly work with the java Smack API, so I followed a mini-tutorial in a java programming forum here:</p>
  
  <ul>
  <li><a href="http://www.javaprogrammingforums.com/java-networking-tutorials/551-how-write-simple-xmpp-jabber-client-using-smack-api.html" rel="nofollow noreferrer">How to Write a simple XMPP (Jabber) client using the Smack API </a></li>
  </ul>
  
  <p>I then changed the code to my needs. The code is posted below:</p>
  
  <pre><code>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{
  
      private XMPPConnection connection;
  
      public void login(String userName, String password) throws XMPPException
      {
          ConnectionConfiguration config = new ConnectionConfiguration("localhost", 5222);
          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&lt;RosterEntry&gt; 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)
      {
          System.out.println("Received something: " + message.getBody());
          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
          System.out.println("-----");
          System.out.println("Login information:");
  
          System.out.print("username: ");
          String login_username = br.readLine();
  
          System.out.print("password: ");
          String login_pass = br.readLine();
  
          c.login(login_username, login_pass);
  
          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);
      }
  
  }
  </code></pre>
  
  <p>In order to run this code, I have the following setup:</p>
  
  <ol>
  <li>An Openfire server running, with two users: admin and user1. </li>
  <li>I enabled all the ports Openfire should require to work as well</li>
  <li>Eclipse IDE, when the code sample. </li>
  </ol>
  
  <p>To run this sample, I run it using Eclipse IDE. I run this application 2 times. First I login with the admin user, and I say I want to contact with user1@openfire.com. Then I run the sample again, as user1, and I say I want to contact with admin@openfire.com.</p>
  
  <p>I have the two samples running at the same time. I can write messages, but the console in the other end never seems to be receiving anything. What am I doing wrong?</p>
  
  <p>I have also checked other similar posts:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/18417216/send-and-receiving-message-using-smack-api">send and receiving message using smack API</a></li>
  <li><a href="https://stackoverflow.com/questions/13858092/unable-to-display-received-messages-with-smack-api-in-java">Unable to display received messages with SMACK api in JAVA</a></li>
  </ul>
  
  <p>However, they are also for specific cases and do not seem to help me, as I would like to have this sample running properly. What am I doing wrong?</p>