Socket and checking for valid host/port

Discussion in 'Java' started by Berlin Brown, Jul 1, 2005.

  1. Berlin  Brown

    Berlin Brown Guest

    Are there ways to check if a host is available or not. It seems like
    Socket doesn't offer this? For example, Socket will fail enventually
    if the host is not there, but it seems like the time is random or at
    least based on the operating system?

    // Wait
    a = Socket("host", 9999);

    // Fail?
     
    Berlin Brown, Jul 1, 2005
    #1
    1. Advertisements

  2. Berlin  Brown

    Berlin Brown Guest

    Actually, I am trying to connect to a TCP/IP server from a Servlet and
    as expected, the servlet locksup with the Socket code.

    What is the simplest way to do Sockets with for example, Tomcat?

    I have heard that I should use MDBs, but this too complex for what I
    need.
     
    Berlin Brown, Jul 1, 2005
    #2
    1. Advertisements

  3. On 30 Jun 2005 20:03:39 -0700, Berlin Brown wrote:
    > Are there ways to check if a host is available or not.


    Yes, by attempting to connect.

    > It seems like Socket doesn't offer this? For example, Socket will
    > fail enventually if the host is not there, but it seems like the
    > time is random or at least based on the operating system?
    >
    > // Wait
    > a = Socket("host", 9999);
    >
    > // Fail?


    Create an unconnected Socket, then call connect() and specify the
    timeout you want:

    Socket s = new Socket();
    SocketAddress sa = new InetSocketAddress("host", 9999);

    s.connect(sa, 10000);

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jul 1, 2005
    #3
  4. Berlin  Brown

    Berlin Brown Guest

    Ok, that is 1.4 code, thanks didnt see that.
     
    Berlin Brown, Jul 1, 2005
    #4
    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. Orpheus66
    Replies:
    0
    Views:
    4,965
    Orpheus66
    Jul 30, 2003
  2. Laszlo Nagy
    Replies:
    1
    Views:
    6,948
    Mark Wooding
    Jan 27, 2009
  3. Steve Holden
    Replies:
    1
    Views:
    971
  4. Jerry Qu
    Replies:
    1
    Views:
    668
    lee atkinson
    Feb 20, 2009
  5. Kr Alfabeta
    Replies:
    13
    Views:
    844
    Slan Dizier
    May 8, 2008
  6. Replies:
    1
    Views:
    866
    Toni Erdmann
    Mar 2, 2005
  7. Andreea
    Replies:
    0
    Views:
    644
    Andreea
    Apr 21, 2012
  8. Ferrous Cranus
    Replies:
    40
    Views:
    596
    Ferrous Cranus
    Feb 22, 2013
Loading...