seeing if a site is up

D

Darren

Hello. I need to use java to check if a server is up and i am trying to do
it with sockets but i understand there are security issues. The aim is this.
There is a members only site that sometimes goes down for mainetenence
This applet needs to contact this site to get a response, any response and
if it does then the site is up. if not then the site is down. Anyone know
how I can acheive this>
TIA
 
M

Matt Humphrey

Darren said:
Hello. I need to use java to check if a server is up and i am trying to do
it with sockets but i understand there are security issues. The aim is
this.
There is a members only site that sometimes goes down for mainetenence
This applet needs to contact this site to get a response, any response and
if it does then the site is up. if not then the site is down. Anyone know
how I can acheive this>

The security issue you're talking about is that fact that an unprivileged
applet can open a connection only to the server from which it came. The
easy "fix" for you in this case is to put the access test into a servlet or
script on the server that hosts the applet--the servlet will not have any
restrictions for reaching the 3rd party machine. The applet can then talk
to your servlet, script, whatever. Alternatively, you can sign the applet
and allow it to request privilege from the user or persuade the user to add
the privilege to the JVM security file--IMHO these are much more difficult.
Further information about applet is here
http://mindprod.com/jgloss/applet.html

Aside from that, just open a connection to the web site and make a request.
You can use HTTPConnection or a direct socket connection, depending on
whichever you find easiest.

The fundamental problem with polling to see if a server is "up", however, is
that the information is really only good at the time of the poll. The
server could go down immediately after the poll. You're probably fine,
though, if you're only interested in whether the server was recently up or
down.

Cheers,
 
J

jan V

Hello. I need to use java to check if a server is up and i am trying to do
it with sockets but i understand there are security issues.

There may also be legal issues. If the server isn't yours, maybe you have no
business testing its status since that may fall well foul of the site's
terms of use. Especially if you start "testing" it at regular intervals....

Depending on the physical location of the server and its legal jurisdiction,
you may also be committing a crime by accessing the machine. You may have
noticed that law enforcement agencies are having a lot more success these
days tracking down people who don't want to respect statutes like the
Computer Misuse Act (applies to the UK).
 
R

Roedy Green

Hello. I need to use java to check if a server is up and i am trying to do
it with sockets but i understand there are security issues. The aim is this.
There is a members only site that sometimes goes down for mainetenence
This applet needs to contact this site to get a response, any response and
if it does then the site is up. if not then the site is down. Anyone know
how I can acheive this>

generally you can get a response from the base site url e.g.
http://mindprod.com

You simulate what a browser does with a GET. See
http://mindprod.com/applets/fileio.html
for how

It will show you the raw sockets way and the HTTPURLConnection way

If that is not sufficient, email me with the word "voter" in the
subject and I will send you some sample code.

To learn more, you want Marty Hall's book, see
http://mindprod.com/jgloss/cgi.html for a link
 
R

Roedy Green

The security issue you're talking about is that fact that an unprivileged
applet can open a connection only to the server from which it came. The
easy "fix" for you in this case is to put the access test into a servlet or
script on the server that hosts the applet--the servlet will not have any
restrictions for reaching the 3rd party machine. The applet can then talk
to your servlet, script, whatever. Alternatively, you can sign the applet
and allow it to request privilege from the user or persuade the user to add
the privilege to the JVM security file--IMHO these are much more difficult.
Further information about applet is here
http://mindprod.com/jgloss/applet.html

Other ways out:

1. signing the applet
2. using JAWS
see http://mindprod.com/jgloss/jaws.html
http://mindprod.com/jgloss/signedapplets.html
 
R

Roedy Green

I need to use java to check if a server is up and i am trying to do
it with sockets but i understand there are security issues.

I assumed you meant HTTP server. If not, then you have to find a
protocol it does support when it is up, e.g. ping or NTP. You Then are
likely involved with Datagrams.
 
D

Darren

jan V said:
There may also be legal issues. If the server isn't yours, maybe you have no
business testing its status since that may fall well foul of the site's
terms of use. Especially if you start "testing" it at regular intervals....

Depending on the physical location of the server and its legal jurisdiction,
you may also be committing a crime by accessing the machine. You may have
noticed that law enforcement agencies are having a lot more success these
days tracking down people who don't want to respect statutes like the
Computer Misuse Act (applies to the UK).
To put your mind at rest the server and all its contents are my personal
property. So what now?
 
J

jan V

To put your mind at rest the server and all its contents are my personal
property. So what now?

Then you've got a big green light. Matt's or Roedy's suggestions should give
you possible approaches to try out.

[BTW, I hope you don;t think I was being funny talking about law. It's not
only high-profile companies like Napster and Kazzaa who end up in court for
falling foul of various legislation]
 
D

Darren

jan V said:
To put your mind at rest the server and all its contents are my personal
property. So what now?

Then you've got a big green light. Matt's or Roedy's suggestions should give
you possible approaches to try out.

[BTW, I hope you don;t think I was being funny talking about law. It's not
only high-profile companies like Napster and Kazzaa who end up in court for
falling foul of various legislation]
No I don't think you were trying to be funny,. It's safe to assume some
hacking attempts are on the go in todays scheme of things . :)
 
D

Darren

Matt Humphrey said:
The security issue you're talking about is that fact that an unprivileged
applet can open a connection only to the server from which it came. The
easy "fix" for you in this case is to put the access test into a servlet or
script on the server that hosts the applet--the servlet will not have any
restrictions for reaching the 3rd party machine. The applet can then talk
to your servlet, script, whatever. Alternatively, you can sign the applet
and allow it to request privilege from the user or persuade the user to add
the privilege to the JVM security file--IMHO these are much more difficult.
Further information about applet is here
http://mindprod.com/jgloss/applet.html

Aside from that, just open a connection to the web site and make a request.
You can use HTTPConnection or a direct socket connection, depending on
whichever you find easiest.

The fundamental problem with polling to see if a server is "up", however, is
that the information is really only good at the time of the poll. The
server could go down immediately after the poll. You're probably fine,
though, if you're only interested in whether the server was recently up or
down.

Cheers,

Well I'm quite happy to do the check on http via GET as the web server
itself needs to be up for the check to proceed.
Here's what i got so far. So where do i go from here?

import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.security.*;

public class Helloserver extends Applet
{
private String hostname = "http://10.0.0.254";
private String port="80";
private URL location;
private Object content;
private String getStr = "GET / HTTP/1.1";



public void init()
{


hello();
}

public void paint(Graphics g)
{

g.drawString(hostname, 50, 60 );
}

/**
* Method hello
*
*
* @return
*
*/
protected final boolean hello()
{
InputStream goGetIt;
String host=hostname+":"+port;
hostname = host;


try
{

location=new URL(hostname);



}
catch(MalformedURLException u)
{
hostname=u.getMessage();
}
catch(IOException u)
{
hostname=u.getMessage();
}


return true;
}


}
 
D

Darren

Roedy Green said:
I assumed you meant HTTP server. If not, then you have to find a
protocol it does support when it is up, e.g. ping or NTP. You Then are
likely involved with Datagrams.
Nothing so complicated though i'll be looking at that in the future. For now
sending and getting a response from my http server will do the trick but how
do I do it in Java?
 
J

Jimi Hullegård

Darren said:
Nothing so complicated though i'll be looking at that in the future. For
now
sending and getting a response from my http server will do the trick but
how
do I do it in Java?

Maybe HTTPClient is useful?
http://www.innovation.ch/java/HTTPClient/

If the code can be very simple for this:

try
{
HTTPConnection con = new HTTPConnection("the-site-to-test.com");
HTTPResponse rsp = con.Get("/test.htm");
if (rsp.getStatusCode() >= 300)
{
/*
the http server is online, but someting went wrong:
if the status code is in the 3xx range, then the request was
redirected (maybe as it should be?)
if the status code is in the 4xx range, then there was a client error,
and 5xx means server error
*/
}
}
catch (IOException e)
{
/*
this happens when the server is down. but it can also happen for some
other reason, like: time out, dns problem, a faulty internet
connection on the client side...
e.getMessage() should give some information. I my tests, I recieved a
ConnectException
(with the message "Connection refused") when the server was down.

To be more certain that there is no problem with the internet
connection on the client side
you can also try to contact a site that you "know" is online.
*/
}

If your server is made out of several different tiers, like Apache for pure
http, and Tomcat as a application server, then you can use the responseCode
to check if Tomcat is up and running smoothly. A 5xx error here would
indicate that Apache is up and running, but there is an issue with Tomcat.

/Jimi
 
D

Darren

Jimi Hullegård said:
Maybe HTTPClient is useful?
http://www.innovation.ch/java/HTTPClient/

If the code can be very simple for this:

try
{
HTTPConnection con = new HTTPConnection("the-site-to-test.com");
HTTPResponse rsp = con.Get("/test.htm");
if (rsp.getStatusCode() >= 300)
{
/*
the http server is online, but someting went wrong:
if the status code is in the 3xx range, then the request was
redirected (maybe as it should be?)
if the status code is in the 4xx range, then there was a client error,
and 5xx means server error
*/
}
}
catch (IOException e)
{
/*
this happens when the server is down. but it can also happen for some
other reason, like: time out, dns problem, a faulty internet
connection on the client side...
e.getMessage() should give some information. I my tests, I recieved a
ConnectException
(with the message "Connection refused") when the server was down.

To be more certain that there is no problem with the internet
connection on the client side
you can also try to contact a site that you "know" is online.
*/
}

If your server is made out of several different tiers, like Apache for pure
http, and Tomcat as a application server, then you can use the responseCode
to check if Tomcat is up and running smoothly. A 5xx error here would
indicate that Apache is up and running, but there is an issue with Tomcat.

What library is "HTTPConnection" in? My hava compiler doesn't recognise it.
 
J

jan V

public class Helloserver extends Applet
{
private String hostname = "http://10.0.0.254";

This is a very poorly chosen identifier. The String contains a URL, not a
host name. Even without the http:// your String would contain a host IP
address, not a host name.
private String port="80";

portNumber would be a better choice. Declaring this as a constant would be
even better, since HTTP normally talks on port 80 and nothing else (in
*normal* circumstances)
private URL location;

serverURL would be better.
private Object content;

pageContent would be better. content on its own is much too vague.
private String getStr = "GET / HTTP/1.1";

httpGETcommand maybe?
g.drawString(hostname, 50, 60 );

Magic constants... not good.
protected final boolean hello()

protected and final together? Doesn't make any sense whatsoever. Look up the
definition of the keywords.. you'll see why.
InputStream goGetIt;

I give up... your identifiers range from poor to attrocious. Maybe that
doesn't bother you now, but it will sooner or later.
 
J

Jimi Hullegård

D

Darren

jan V said:
This is a very poorly chosen identifier. The String contains a URL, not a
host name. Even without the http:// your String would contain a host IP
address, not a host name.


portNumber would be a better choice. Declaring this as a constant would be
even better, since HTTP normally talks on port 80 and nothing else (in
*normal* circumstances)


serverURL would be better.


pageContent would be better. content on its own is much too vague.


httpGETcommand maybe?


Magic constants... not good.


protected and final together? Doesn't make any sense whatsoever. Look up the
definition of the keywords.. you'll see why.


I give up... your identifiers range from poor to attrocious. Maybe that
doesn't bother you now, but it will sooner or later.

You don't like my identifiers? God forbid you see my C code then. :)
Seriously my identifiers are chosen because they are suitable and readable
to me and after all this is not what I was asking though your point about
protected with final is accepted.
 
D

Darren

Jimi Hullegård said:
Darren said:
Jimi Hullegård said:
For now sending and getting a response from my http server
will do the trick but how do I do it in Java?

Maybe HTTPClient is useful?
http://www.innovation.ch/java/HTTPClient/
[...]

What library is "HTTPConnection" in? My hava compiler doesn't recognise
it.

Did you visit the link? --> http://www.innovation.ch/java/HTTPClient/
There is a download link on that page.
I just did. where do you unpack the files too? I'm assuning theres a
location windows keeps its java classes
 
D

Dag Sunde

Darren said:
Jimi Hullegård said:
Darren said:
For now sending and getting a response from my http server
will do the trick but how do I do it in Java?

Maybe HTTPClient is useful?
http://www.innovation.ch/java/HTTPClient/
[...]

What library is "HTTPConnection" in? My hava compiler doesn't recognise
it.

Did you visit the link? --> http://www.innovation.ch/java/HTTPClient/
There is a download link on that page.

I just did. where do you unpack the files too? I'm assuning theres a
location windows keeps its java classes

Why download something when you already have everything you need?

try {
// Create a URL for the desired page
URL url = new URL(http://hostname:80/index.html);

// Read all the text returned by the server
BufferedReader in = new BufferedReader(new
InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
...
} catch (IOException e) {
...
}

now, if str contains the content of "index.html", your server is up...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top