• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Check for Internet

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
}
/********************************************************/
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic