Establishing Socket Connection to a slow or busy server

C

Chad W. Taylor

Currently, I'm developing an AIM-based BOT application in Java and it
requires establishing a socket connection to their free and public
server (host: toc.oscar.aol.com, port: 9898).

About 75% of the time a connection will be successfully established
using this:

oConnection = new Socket("toc.oscar.aol.com", 9898);

25% of the time when a connection fails (timeout) is due to the server
being too slow or busy to respond to the connection request; however,
the server will eventually response within 40 to 60 seconds.

Let me go into details with my findings when attempting to connect to
the slow/busy server:

For experimental purposes, I used the telnet command (via the DOS
command prompt: "telnet toc.oscar.aol.com 9898"), the connection will
be established usually within 40 to 60 seconds.

As I said before, the BOT application is developed in a Java
environment and when attempting to establish a connection (using
Java's Socket), a timeout exception gets raised when it hits the
20-seconds mark. It tells me that the Java Socket has the timeout
defaulted to 20-seconds.

I am aware that we can define the timeout settings using Socket's
"setSoTimeout(x)" method; however, it's only good for AFTER a
connection is established.

Now, to sum up my findings – it clearly shows that the DOS' telnet
prompt has longer "wait time" before raising any exceptions. As far
as Java Socket is concerned, if a connection is not established within
20 seconds, it raises the timeout exception.

Is there a way to stretch the "wait time" or "timeout" to be longer
than 20 seconds for when a socket connection is being attempted?

Millions of thanks in advance,
Chad W. Taylor
 
I

iksrazal

Is there a way to stretch the "wait time" or "timeout" to be longer
than 20 seconds for when a socket connection is being attempted?

Millions of thanks in advance,
Chad W. Taylor

The big problem using the standard Socket classes up until jdk 1.3 is
their inflexibility. jdk 1.4 added the nio classes which perform much
like the C/unix model. The disadvantage of the nio classes is that
they are harder to program. The advantage is the flexibility needed to
solve most problems.

At the moment I'm designing a scalable client socket architecture for
a protocol similair to telnet. I do have some problems still as
referenced by my other posts, but I like a lot what I'm getting out of
nio so far.

To answer your question, with nio you can have a timeout to recieve
the connection, but then it is no longer asynchronous/non-blocking.
That might not matter for you, but if it does you can use
java.util.timer . See my post on this group about nio and timeouts. As
for nio reads/writes, see my current post that I'm trying to resolve.
In the end, nio allows for timeouts on both recieving the connection
and seperately for doing read/writes.

If you decide not to go with nio, try this:

Socket.connect(SocketAddress, 30000);

HTH

Outsource to an American programmer living in brazil!
http://www.braziloutsource.com/
iksrazal
 

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

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,842
Latest member
MableIwk73

Latest Threads

Top