• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check for Internet

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an application that requires connection to the Web. Does anyone know how I can check for a connection in my JAVA app so that I know whether or not to instruct the user to make a connection before continuing?
Thanks

------------------
Happy Coding,
Gregg Bolinger
 
aditya mahajan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to check whether you have a connection ar not.
Let the program itsel connect to the web without the user having to instruct it. Or you can ask the user before connecting for his consent a OK - CANCEL type of alert
Aditya Mahajan
Originally posted by Gregg Bolinger:
I have an application that requires connection to the Web. Does anyone know how I can check for a connection in my JAVA app so that I know whether or not to instruct the user to make a connection before continuing?
Thanks

 
Ravi Verma
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works in this way, It return code 200 if you are connected to Internet else return code is 401,404 etc.,
/*********************************************/
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
public class SourceViewer {
public static void main(String[] args){

try{
URL u = new URL("http://www.yahoo.com/");
System.out.println("Host...."+u.getHost());
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
int code = uc.getResponseCode();
System.out.println("Return Code....."+code);
}
catch(IOException e){
e.printStackTrace();
System.err.println(e);
}
}
}
/********************************************************/
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checking for an internet connection can cause at least one problem I know of. When I am offline and the computer set as the gateway is not turned on, it can take a good 20-30 seconds for a connection to fail. Just thought I'd bring up the topic. I don't really know what causes it though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic