wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Check for Internet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Check for Internet" Watch "Check for Internet" New topic
Author

Check for Internet

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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


GenRocket - Experts at Building Test Data
aditya mahajan
Greenhorn

Joined: Jul 13, 2001
Posts: 8
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

Joined: Aug 30, 2001
Posts: 42
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);
}
}
}
/********************************************************/

Ravi
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
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.
 
Consider Paul's rocket mass heater.
 
subject: Check for Internet
 
Similar Threads
Connection Pool Problem in weblogic 9.2 MP3
DBCP could not obtain an idle db connection, pool exhausted
how can i handle that user close the web browser?(javascript)
Connection validity check
Connection Statsus - Help Please