• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem in checking net connection  RSS feed

 
Prasanna RamKumar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i have some problem in finding whether net connection is established or not
is there anything wrong in my code

import java.net.*;
import java.io.*;

/**
*
* @author prasanna
*/
public class ConnectionDetector {

/** Creates a new instance of ConnectionDetector */
public ConnectionDetector() {
}


public static void main(String arr[])
{
ConnectionDetector c =new ConnectionDetector();
try
{
c.detect();
}
catch(Exception e)
{
System.out.println(""+e);
e.printStackTrace();
}
}


public void detect() throws IOException
{


HttpURLConnection httpCon=null;
URL hp=null;

int statusCode;
String statusMessage;


try
{
System.out.println("inside try");


hp= new URL("http://www.google.com");
httpCon=(HttpURLConnection)hp.openConnection();
System.out.println("HOST"+ hp.getHost());



statusCode = httpCon.getResponseCode();
statusMessage=httpCon.getResponseMessage();
System.out.println(""+statusCode);
System.out.println(""+statusMessage);

if(statusCode==200)
{
System.out.println("Connected to ISP");


}
else if(statusCode==-1)
{
System.out.println("Cant connect to ISP["+statusMessage+"]");

}
else
{
System.out.println("ISP connected but different status["+statusMessage+"]");

}
}
catch(MalformedURLException e)
{
System.out.println(" - "+e);
}
catch(UnknownHostException e)
{
System.out.println("Network Cable Unplugged - "+e);

}catch(NoRouteToHostException e)
{
System.out.println("Network Cable Unplugged - "+e);
}
catch(Exception e)
{
System.out.println("- "+e);
//e.printStackTrace();
}


}//end of detect method


}

please help me to find a solution
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say you have a problem, but you don't tell us what it is. As our FAQ says, Tell The Details.
One thing you should know is that HttpURLConnetion.getResponseCode() returns the status of the HTTP conversation, not the connection itself. Those codes and their meanings are documented in the API.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!