This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (5 pts)
1 Replies Last post: Jun 27, 2008 8:25 PM by Ryan Graham  
Harshit Bronze 4 posts since
Jun 27, 2008
Currently Being Moderated

Jun 27, 2008 5:04 PM

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

             &n bsp;  this.acceptor.addConnection(conn);

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

             &n bsp;  System.out.println("Connected");

 

             &n bsp;  //Thread.sleep(50000);

             &n bsp;  this.connection = conn;

             &n bsp;  //break;

            } catch (Exception ex) {

 

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

             &n bsp;  XMPPConnection tmp = new XMPPConnection(cc);

             &n bsp;  try {

             &n bsp;      tmp.connect();

             &n bsp;      System.out.println("Redirected");

             &n bsp;      AccountManager am = tmp.getAccountManager();

             &n bsp;      System.out.println("1");

             &n bsp;      String password = this.createNewPassword(this.config.getUserName());

 

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

 

             &n bsp;      try {

             &n bsp;      am.createAccount(this.config.getUserName(), password);

             &n bsp;      tmp.login(this.config.getUserName(), password);

             &n bsp;      } catch(Exception e) {

             &n bsp;          e.printStackTrace();

             &n bsp;          System.out.println("why " +e);

             &n bsp;      }

 

             &n bsp;      System.out.println("work done");

             &n bsp;      //this.config.storePassword(password);

             &n bsp;  } catch (Exception e) {

             &n bsp;      System.out.println("smack exception");

             &n bsp;      throw new SignalingException(e);

             &n bsp;  } finally {

             &n bsp;      System.out.println("1st disconnect");

             &n bsp;      tmp.disconnect();

             &n bsp;  }

            } finally {

             &n bsp;  if (conn != null && !conn.isAuthenticated()) {

             &n bsp;      System.out.println("2nd disconnect");

             &n bsp;      conn.disconnect();

             &n bsp;  }

            }

        }

-


 

 

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

 

 

Ryan Graham KeyContributor 1,726 posts since
Jan 17, 2003
Currently Being Moderated
Jun 27, 2008 8:25 PM in response to: Harshit
Re: Not able to stay connected

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.

More Like This

  • Retrieving data ...