title: Sending and Receiving messages using Smack Api for Android
body: <p>I'm trying since last four days to send and receive chat message using own XMPP and with Smack+OpenFire. According to Smack's "readme.txt' i set up the connection and got logged user in. The code of connection and login is this</p>
  
  <pre><code>public static String TAG = "Test connection";
  private static XMPPTCPConnection connection;
  private static String userName = "demo";
  private static String Password = "demo";
  
  static {
      // Create the configuration for this new connection
      XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
      configBuilder.setSecurityMode(XMPPTCPConnectionConfiguration.SecurityMode.disabled);
      configBuilder.setResource("");
      configBuilder.setUsernameAndPassword(userName, Password);
      configBuilder.setServiceName("192.168.2.10");
      configBuilder.setHost("192.168.2.10");
      configBuilder.setPort(5222);
      configBuilder.setCompressionEnabled(false);
      connection = new XMPPTCPConnection(configBuilder.build());
  }
  </code></pre>
  
  <p>This way i configured the connectionbuilder.
  here i am connecting and signing in the user.</p>
  
  <pre><code>public class ConnectAndLogin extends AsyncTask&lt;Void, Void, Void&gt; {
      @Override
      protected Void doInBackground(Void... params) {
          try {
              connection.connect();
              Log.i(TAG, "Connected to " + connection.getHost());
          } catch (SmackException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (XMPPException e) {
              Log.e(TAG, "Failed to connect to " + connection.getHost());
              Log.e(TAG, e.toString());
              e.printStackTrace();
          }
  
          try {
              connection.login(userName, Password);
              Log.i(TAG, "Login as a : " + connection.getUser());
              setConnection(connection);
              setListner();
          } catch (XMPPException e) {
              e.printStackTrace();
              Log.i(TAG, "Login error " + e.toString());
          } catch (SmackException e) {
              e.printStackTrace();
              Log.i(TAG, "Login error " + e.toString());
          } catch (IOException e) {
              e.printStackTrace();
              Log.i(TAG, "Login error " + e.toString());
          }
  
  
          return null;
      }
  }
  </code></pre>
  
  <p>In some tutorials that the <code>addPacketListner</code> must be set after login. i done that in <code>setConnection()</code> and some posts went through <code>addAsyncStanzaListner</code>.
  <strong>for send message</strong></p>
  
  <pre><code>send.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Message msg = new Message("demo2", Message.Type.chat);
              msg.setBody("Hi how are you");
              if (connection != null) {
                  try {
                      connection.sendPacket(msg);
                      Log.d("Send to room  : Name : ", "demo2");
                      Log.d("store", "store data to db");
                      //DBAdapter.addUserData(new UserData(text, "", "1" ,beam_id));
                  } catch (Exception e) {
                      Log.d("ooo", "msg exception" + e.getMessage());
                  }
              }
          }
      });
  </code></pre>
  
  <p>and for <strong>receiving messages</strong> this code</p>
  
  <pre><code> public void setListner() {
      connection.addAsyncStanzaListener(new StanzaListener() {
          @Override
          public void processPacket(Stanza packet) throws SmackException.NotConnectedException {
              Message message = (Message) packet;
              Log.i(TAG, "REC : " + message.getBody());
              Log.i(TAG, "REC: " + message.getFrom());
  
  
          }
  
      }, new StanzaFilter() {
          @Override
          public boolean accept(Stanza stanza) {
              return false;
          }
      });
  }
  </code></pre>
  
  <p>here is my dependencies</p>
  
  <pre><code> compile 'org.igniterealtime.smack:smack-android:4.1.0'
  compile 'org.igniterealtime.smack:smack-tcp:4.1.0'
  compile 'org.igniterealtime.smack:smack-core:4.1.0'
  compile 'org.igniterealtime.smack:smack-im:4.1.0'
  compile 'org.igniterealtime.smack:smack-resolver-minidns:4.1.0'
  compile 'org.igniterealtime.smack:smack-sasl-provided:4.1.0'
  </code></pre>
  
  <p>But still i'm not able to send messages to demo2 user. 
  Where im doing wrong.Please guide and help me.
  Thanks</p>