Socket Lag

If I run my client and then run my server, it takes like 5 seconds for my client to realize that the server is there?How do I get it to retry its connection more often?
[182 byte] By [Nethera] at [2007-11-27 3:54:02]
# 1
Have it timeout more quickly.
DrLaszloJamfa at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 2
how do i do that
Nethera at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 3
[url= http://java.sun.com/javase/6/docs/api/java/net/Socket.html#setSoTimeout(int)]setSoTimeout(millis)[/url]
DrLaszloJamfa at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 4
But I can't call a method on an object that hasnt finished constructing, right? It halts on the constructor line of the code.
Nethera at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 5

Er, that timeout is for reading, now that I think about it. Maybe this version of connect is the answer: [url=http://java.sun.com/javase/6/docs/api/java/net/Socket.html#connect(java.net.SocketAddress,%20int)]connect(SocketAddress, timeout)[/url]:

Socket s = new Socket(); //note: no args

s.connect(socketAddess, timeout);

...

DrLaszloJamfa at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 6

Aha:

Socket mySocket = new Socket();

//set timeout to 5 seconds

mySocket.setSoTimeout(5000);

InetSocketAddress nextport = new InetSocketAddress(hostname, port);

try {

mySocket.connect(nextport);

} catch (SocketTimeoutException ste) {

ste.printStackTrace();

}

Nethera at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 7
I think we're converging on an answer! Could you try these various things and post your findings?
DrLaszloJamfa at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 8
i'm testing them now :)
Nethera at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 9
The code i posted works like a bee.
Nethera at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 10

What exactly do you mean by '5 seconds for my client to realize that the server is there'?

> //set timeout to 5 seconds

> mySocket.setSoTimeout(5000);

That's the read timeout, not the connect timeout.

> InetSocketAddress nextport = new InetSocketAddress(hostname, port);

> try {

>mySocket.connect(nextport);

You probably want to supply a connect timeout in this call.

ejpa at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 11
> You probably want to supply a connect timeout in this call.Woohoo! That would be reply #5
DrLaszloJamfa at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 12
yes it would Dr Jamf
ejpa at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...
# 13
> yes it would Dr JamfI did a little, Dilbert-like dance in my cubicle.
DrLaszloJamfa at 2007-7-12 8:58:08 > top of Java-index,Java Essentials,Java Programming...