Connection Timeout

P

pselibas

Good day...

I have writen a method fo a program that checks to see if a port is
open on a specific host every 10 minutes to see if a particular service
is running.

public static boolean isPortUp(String host, int port){
boolean ret = true;
Socket MyClient = new Socket();
InetSocketAddress socketAddress = new InetSocketAddress(host,
port);
try {
MyClient.connect(socketAddress, 9000);
MyClient.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
ret = false;
}
return ret;
}

This method works perfectly well at first. Once the host goes down and
comes back up and this method i called again, an Exception is thrown
(Connection timeout). Does anyone know why this is and how i can fix
it.

Many Thanks.
 
B

Benji

Good day...
public static boolean isPortUp(String host, int port){
boolean ret = true;
Socket MyClient = new Socket();
<snip>

oh goodness, don't start variables with capital letters.

Also, I don't know if you can reuse a Socket object like that. (I doubt
it) Try recreating the socket every time and see if that fixes it.
 
P

pselibas

Correct me if i am wrong but surely the Object is being recreated
everytime the method is called.

I do apologise about breaching the naming convention.
 
R

Roedy Green

This method works perfectly well at first. Once the host goes down and
comes back up and this method i called again, an Exception is thrown
(Connection timeout). Does anyone know why this is and how i can fix
it.

Do you have any evidence that the host is actually accepting
connections at the time of your new test? Maybe you just probed it
before it was quite ready.
 
B

Benji

Correct me if i am wrong but surely the Object is being recreated
everytime the method is called.

holy cow, you're right, I don't know what I was thinking.

So, I can't see anything that looks wrong, then...what's the problem?
It seems like that's the exception that should be getting thrown if
the server is not up. So, does the remote server shut down, start up
(completely), and *then* you try to connect to it and it throws that
exception? Or does it throw the exception while the server is down and/or
starting up? (that would be what I would expect)
 
A

Andrew Thompson

I do apologise about breaching the naming convention.

Most people would prefer to hear "I'll try to remember
to use the common convention when posting".

[ No 'apology' is necessary. ]
 
P

pselibas

The method works fine when the server is down BUT the problem comes
when the server comes back up after it has been down, then the method
still catches a Connection timeout. When i restart my java program its
fine again.

Put it this way.. i could be checking a web server on port 80.
1pm --- Server is up and program says its online.
2pm --- Server is down and program says its offline.
3pm --- Server is up and program says its offline.
I manually restart the program
4pm --- Server is up and program says its online.

I cant even think of an alternative solution to debug the problem.
Maybe there is someway of clearing the exception... But to my knowledge
this is not possible or necessary.
 
R

Roedy Green

I cant even think of an alternative solution to debug the problem.

You could open the connection twice and take the result of the second
open. Perhaps something between you and the server is caching old
information.
 
T

Thomas Hawtin

I have writen a method fo a program that checks to see if a port is
open on a specific host every 10 minutes to see if a particular service
is running.

public static boolean isPortUp(String host, int port){
boolean ret = true;

It's generally (but not exclusively) considered better to just return
the value straight, rather than going through an unnecessary variable.
At least put the ret - true after the close and give it a descriptive name.
Socket MyClient = new Socket();

It helps readability to stick to the code conventions.
InetSocketAddress socketAddress = new InetSocketAddress(host,
port);

Possibly the IP address is specified by DHCP and you get a different one
on rebooting. Also, are you sure that the server actually does
successfully bind the listening socket?
try {
MyClient.connect(socketAddress, 9000);
MyClient.close();
}
catch (Exception e) {

It's not a good idea to catch all exceptions. IOException is the one
that you want.
System.out.println(e.getMessage());
ret = false;
}
return ret;
}

Tom Hawtin
 
P

pselibas

Okay i think i might have found the problem...

<quote>
Two Java security properties control the TTL values used for positive
and negative host name resolution caching:

networkaddress.cache.ttl (default: -1)
Indicates the caching policy for successful name lookups from
the name service. The value is specified as as integer to indicate the
number of seconds to cache the successful lookup.

A value of -1 indicates "cache forever".

networkaddress.cache.negative.ttl (default: 10)
Indicates the caching policy for un-successful name lookups
from the name service. The value is specified as as integer to indicate
the number of seconds to cache the failure for un-successful lookups.

A value of 0 indicates "never cache". A value of -1 indicates
"cache forever".


</quote>

That is straight from the java API docs. The quest is how do i change
the values of those properties?
 
C

chris_k

Hi,
That is straight from the java API docs. The quest is how do i change
the values of those properties?

under your [JAVA_HOME]/jre/lib/security folder find and modify file
java.security

HTH,
chris
 
P

pselibas

Thank you very much. Although i do find this a bit irritating because
you would have to change that value on every platform you run the
program on.

I found this and i am was wondering if it does the same thing?

java.security.Security.setProperty("networkaddress.cache.ttl" , "0");

Many thanks to all who contributed.

Paul Selibas
 
P

pselibas

We will just have to wait and see... The server has not gone down yet.
I am holding thumbs that it will work.
 
C

chris_k

Didn't try it myself but setting it at run-time should be working fine.
If you see the source of java.security.Security, it loads the content
of file "java.security" into a Properties. Then if you invoke (if yor
app. havs rights to do this) Security.setProperty(key, val) the content
of the Properties map is updated.

We will just have to wait and see... The server has not gone down yet.
I am holding thumbs that it will work.

chris
 

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
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top