Establishing Socket Connection to a slow or busy server

Discussion in 'Java' started by Chad W. Taylor, Jun 25, 2004.

  1. 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:, port: 9898).

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

    oConnection = new Socket("", 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 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
    Chad W. Taylor, Jun 25, 2004
    1. Advertisements

  2. iksrazal

    iksrazal Guest

    (Chad W. Taylor) wrote in message news:<>...
    > 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);


    Outsource to an American programmer living in brazil!
    iksrazal, Jun 26, 2004
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dGhvbQ==?=

    error while establishing a connection to the server

    =?Utf-8?B?dGhvbQ==?=, Jul 25, 2006, in forum: ASP .Net
    Jul 25, 2006
  2. Tor Inge Rislaa
    Tor Inge Rislaa
    Sep 27, 2006
  3. Mangabasi
    Apr 23, 2007
  4. Replies:
    Gordon Beaton
    Dec 12, 2007
  5. Joe Van Dyk
    Adam P. Jenkins
    Jul 6, 2005

Share This Page