Testing for an existing connection

Hello all,

I was trying to determine if a connection exists by using the connection.isConnected() method. Not sure if this is even the correct way to test if connected? Any advice is appreciated!

public void getConnection(String user, String pwd) throws XMPPException

{

conn = new XMPPConnection(HOST,PORT);

if(conn.isConnected()){System.out.println(“CONNECTED FROM WEBCONNECT”);}

else{

System.out.println(“NOT CONNECTED FROM WEB CONNECT”);

conn.DEBUG_ENABLED = true;

conn.login(user,pwd);

}

}

/code

Basically what I am trying to do is to determine if the user is already connected and if not, create a connection.

TIA!!

I should have added the following:

I also tried the following: (called from a jsp.) which throws an error (see below).

public void getConnection(String user, String pwd) throws XMPPException

{

if(conn.isConnected()){System.out.println(“CONNECTED FROM WEBCONNECT”);}

else{

conn = new XMPPConnection(HOST,PORT);

System.out.println(“NOT CONNECTED FROM WEB CONNECT”);

conn.DEBUG_ENABLED = true;

conn.login(user,pwd);

}

}

/code

java.lang.NullPointerException

chatPackage.WebConnect.getConnection(WebConnect.java:37)

line 37 is:

if(conn.isConnected()){System.out.println(“CONNECTED FROM WEBCONNECT”);}

/code

TIA!

Hi,

you should check whether conn == null before trying to test conn.isConnected() like:

if ( ( conn != null ) && ( conn.isConnected() ) )

What was the problem with the code of your first post?

LG

I wasnt sure that it would work properly. I was creating the conn object and then testing to see if it was connected. Wasnt sure where the connection is actually made:

conn = new XMPPConnection(HOST,PORT); //here

conn.login(user,pwd); //or here

/code

Comments? Suggestions?

Thanks alot.

Graham

Hey

From the JavaDocs

isConnected

public boolean isConnected()

// Returns true if currently connected to the XMPP server.

// Returns: true if connected.

/code

and

isAuthenticated

public boolean isAuthenticated()

//Returns true if currently authenticated by successfully calling the login method.

//Returns:true if authenticated.

/code

hth

Jon