× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



> -----Original Message-----
> From: java400-l-admin@midrange.com
> [mailto:java400-l-admin@midrange.com] On Behalf Of Hall, Philip
> Sent: Monday, July 29, 2002 7:10 PM
> To: java400-l@midrange.com
> Subject: RE: Sockets question...
>
>
> Mike,
>
> > -----Original Message-----
> > From: Mike Silvers [mailto:msilvers@HBS-INC.COM]
> > Is there any way to shorten the time for a connect timeout?
>
> Not easy (or even not possible without writing some JNI code)
> in JDK 1.3.x
>

Hmmm...  I think this will work without JNI code:

In your client:

  long timeoutValue = 100;   // .1 seconds
  Socket obtainedSocket;

  ConnHelper ch = new ConnHelper(host, port);
  ch.start();
  try
  {
    ch.join(longTimeoutValue);
  }
  catch (InterruptedException ie) {}

  if (ch.getException() != null)
  {
    throw ch.getException();
  }

  obtainedSocket = ch.getSocket();
  if (obtainedSocket == null)
  {
    ch.forgetIt();   // Don't want it now
    obtainedSocket = ch.getSocket();
    if (obtainedSocket != null)  // Snuck in
    {
      obtainedSocket.close();
    }
    throw new InterruptedIOException("Connection timeout.");
  }

  // At this point, obtainedSocket can safely be used.

Here is ConnHelper:

  public class ConnHelper extends Thread
  {
    IOException m_exception;
    Socket      m_socket;
    boolean     m_forgetIt;

    public ConnHelper(String host, int port)
    {
      super("ConnHelper (" + host + ":" + port + ")");
      try
      {
        setDaemon(true);
      }
        catch (SecurityException se) {}
    }

    public void run()
    {
      try
      {
        m_socket = new Socket(host, port);
      }
      catch (IOException ioe)
      {
        m_exception = ioe;
      }

      if (m_forgetIt && (m_socket != null))
      {
        try
        {
          m_socket.close();
        }
        catch (IOException ioe) {}
        m_socket = null;
      }
    }

    public IOException getException()
    {
       return m_exception;
    }

    public Socket getSocket()
    {
       return m_socket;
    }

    public void forgetIt()
    {
       m_forgetIt = true;
    }
  }

I haven't actually tried this but I think it will work.

HTH,
Gary



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.