By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

is system connected to the internet?

P: n/a
Hello to all,

Can you give me the code so that i can make sure that my system is
connected to the internet.
thank you in advance,
vishnu
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
vi*************@yahoo.com (vishnu mahendra) wrote in message news:<c2*************************@posting.google.c om>...
Hello to all,

Can you give me the code so that i can make sure that my system is
connected to the internet.
thank you in advance,
vishnu


Send some client request or do Ping to one of well known servers.
Jul 17 '05 #2

P: n/a
You can use ping at the OS level, otherwise use code below to visit
well-known website using HTTP. If browsed, method visitWebPage() returns
true, otherwise false. False can be website is down or internet connection
unavailable.
import java.io.*;
import java.net.*;

public class UrlVisit {

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

public boolean visitWebPage(String UrlToVisit) {

boolean urlVisited = false;
try {
URL url = new URL(UrlToVisit);
URLConnection urlConnection = url.openConnection();
BufferedReader htmlPage = new BufferedReader(new
InputStreamReader(url.openStream()));

String line = "";
while((line = htmlPage.readLine()) != null) {
//do something with the html line
System.out.println(line);
}
htmlPage.close();
urlConnection = null;
urlVisited = true;
} catch(Exception e) {
urlVisited = false;
}
return urlVisited;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
UrlVisit urlVisit = new UrlVisit();
String url = null;
String commstart = " <!-- *********************** ";
String commend = " *********************** -->";
if (args[0] == null) {
url = "http://www.cnn.com";
} else {
url = args[0];
System.out.println(commstart + "Visiting "+ url + commend);
}
if (urlVisit.visitWebPage(url)) {
System.out.println(commstart + url + " visited" + commend);
} else {
System.out.println(commstart + url + " Website not found" +
commend);
}
}
}

"vishnu mahendra" <vi*************@yahoo.com> wrote in message
news:c2*************************@posting.google.co m...
Hello to all,

Can you give me the code so that i can make sure that my system is
connected to the internet.
thank you in advance,
vishnu

Jul 17 '05 #3

P: n/a
"JavaJunkie" <ja********@hotmail.com> wrote in message news:<_h*********************@twister.tampabay.rr. com>...
You can use ping at the OS level, otherwise use code below to visit
well-known website using HTTP. If browsed, method visitWebPage() returns
true, otherwise false. False can be website is down or internet connection
unavailable.
import java.io.*;
import java.net.*;

public class UrlVisit {

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

public boolean visitWebPage(String UrlToVisit) {

boolean urlVisited = false;
try {
URL url = new URL(UrlToVisit);
URLConnection urlConnection = url.openConnection();
BufferedReader htmlPage = new BufferedReader(new
InputStreamReader(url.openStream()));

String line = "";
while((line = htmlPage.readLine()) != null) {
//do something with the html line
System.out.println(line);
}
htmlPage.close();
urlConnection = null;
urlVisited = true;
} catch(Exception e) {
urlVisited = false;
}
return urlVisited;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
UrlVisit urlVisit = new UrlVisit();
String url = null;
String commstart = " <!-- *********************** ";
String commend = " *********************** -->";
if (args[0] == null) {
url = "http://www.cnn.com";
} else {
url = args[0];
System.out.println(commstart + "Visiting "+ url + commend);
}
if (urlVisit.visitWebPage(url)) {
System.out.println(commstart + url + " visited" + commend);
} else {
System.out.println(commstart + url + " Website not found" +
commend);
}
}
}



Thank you Mr.JavaJunkie.
I love groups.
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.