• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in checking net connection

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic