aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Socket connections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket connections" Watch "Socket connections" New topic
Author

Socket connections

Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
Hi, I'm having a little trouble with Socket's!
public boolean isConnected(String ipAddress)
{
final int dayTimePort = 13;
final int pingPort = 7003;
final int telnetPort = 23;
BufferedReader in = null;
Socket socketComputer = null;
try
{
socketComputer = new Socket(ipAddress, dayTimePort);
//in = new BufferedReader(new InputStreamReader(socketComputer.getInputStream()));
//String timeStamp = in.readLine();
return true;
}
catch(UnknownHostException e)
{
return false;
}
catch(IOException e)
{
return false;
}
}
This is used to check whether any of our critical systems have gone down, the boolean returned is used to determine what image and set of actions to do/display on the intranet site I'm developing.
The above code works fine, however testing it by giving it a fake ipaddress, i.e. one that doesn't exists( to act as a 'down' system) - the socket connection takes ages to conclude that it can't make a connection. This causes trouble by causing the page to display very very slowly.
Can anyone think of a way of improving this?
Any help would be very much appreciated!
Rowan.


The early bird may get the worm, but the second mouse gets the cheese.........<br /> <br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Web-Component Developer for J2EE Platform
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The socket is spending an awful lot of time trying to establish the TCP connection to the target IP, and I don't know of a way to tweak this (although such a way may well exist).
But really, what seems to be getting in your way is that TCP is an intelligent protocol that handshakes with the other end to guarantee packet delivery, etc. The problem may be more suited to datagrams. You can use a DatagramSocket to send a DatagramPacket to the remote host, then try to receive() a response packet for a limited amount of time (see DatagramSocket.setSoTimeout() -- I assume there will be a DatagramSocket sitting at the other end to actually send that response). Beware that datagram delivery is not guaranteed, so unless you're on a LAN you will want to try a few times before concluding that the remote host is down.
- Peter
[ May 10, 2002: Message edited by: Peter den Haan ]
Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
Many thanks, I'll shall try that!
Rowan.
Maalti Iyer
Ranch Hand

Joined: Jul 11, 2001
Posts: 52
Try using the method setSoTimeOut(long sec) on the socket object - I believe, the default value is zero (indefinite wait)
I hope this helps.
Maalti.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Socket connections