Not able to stay connected

Hello Friend,

This is the code I m using :

ConnectionConfiguration cc = new ConnectionConfiguration(this.config.getHost(),this.config.getPort());

System.out.println(“Reached in DeviceSignalingChannel init()”);

while (true) {

XMPPConnection conn = new XMPPConnection(cc);

try {

conn.connect();

this.acceptor.addConnection(conn);

conn.login(this.config.getUserName(), this.config.getPassword());

System.out.println(“Connected”);

//Thread.sleep(50000);

this.connection = conn;

//break;

} catch (Exception ex) {

System.out.println("After connected "+ex );

XMPPConnection tmp = new XMPPConnection(cc);

try {

tmp.connect();

System.out.println(“Redirected”);

AccountManager am = tmp.getAccountManager();

System.out.println(“1”);

String password = this.createNewPassword(this.config.getUserName());

System.out.println("check here " +password);

try {

am.createAccount(this.config.getUserName(), password);

tmp.login(this.config.getUserName(), password);

} catch(Exception e) {

e.printStackTrace();

System.out.println("why " +e);

}

System.out.println(“work done”);

//this.config.storePassword(password);

} catch (Exception e) {

System.out.println(“smack exception”);

throw new SignalingException(e);

} finally {

System.out.println(“1st disconnect”);

tmp.disconnect();

}

} finally {

if (conn != null && !conn.isAuthenticated()) {

System.out.println(“2nd disconnect”);

conn.disconnect();

}

}

}


When I run the above program, it runs, connectes to the XMPP server , logs in successfully and immediately disconnects.

if I use Thread.sleep(milliseconds) , then it will wait for those miliseconds, but i don’t want that.

What I want is : it should be connected for endless time unless I call the XMPPConnection’s disconnect() method.

I tried it putting in while(true) loop, but it didn’t work

Please help me.

Thanks

Hi hnmapara,

Your client is disconnecting because your application is terminating. It looks like you have a lot of different things going on in your code so I would suggest you start off with a simple application like the following:

import javax.swing.JOptionPane; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; public class SimpleSmack {
   public static void main(String[] args) {
      new SimpleSmack();
   }
      private SimpleSmack() {
      XMPPConnection con = null;
      try {
         con = new XMPPConnection("localhost");
         con.connect();
         con.login("jack", "1");
                  JOptionPane.showMessageDialog(null, "Click ok to disconnect");
      } catch (XMPPException e) {
         e.printStackTrace();
      } finally {
         if (con != null) {
            con.disconnect();
         }
      }
   }
}

Hope that helps,

Ryan

PS - Since this is more of a Smack question I would recommend you post to any future questions to smack forum.