nonblocking sockets

Discussion in 'Java' started by nooneinparticular314159@yahoo.com, Apr 7, 2006.

  1. Guest

    I have a client that is talking to a server. I want to use nonblocking
    sockets. The client connects fine, so long as the server is already
    up. But the client doesn't connect if the server isn't already up (of
    course). Assuming that the server isn't up, I want the client to wait
    a few seconds and try again. But for some reason, in my code, once the
    client has failed to connect once, further attempts always fail. In
    other words, in the code below, finishConnect() should return true once
    a connection has been established. If the server is already up, it
    does return true, and everything works. If the server is down,
    finishConnect() returns false (as it should). But then subsequent
    calls to it always return false, so the connection is never
    established. Any idea what I'm doing wrong? What is the proper way to
    use this?

    Thanks!

    //Create a socket to connect to teh server
    InetSocketAddress InternetSocketAddress = new
    InetSocketAddress(ServerAddress, Port);

    SocketChannel ClientSocketChannel = null;
    try{
    ClientSocketChannel = SocketChannel.open();
    ClientSocketChannel.configureBlocking(false);
    }catch (IOException Exception) {
    System.out.println("Could not open SocketChannel on
    client");
    System.exit(-1);
    }

    try{
    ClientSocketChannel.connect(InternetSocketAddress);
    }catch (IOException Exception) {
    System.out.println("Could not connect to server");
    }
    Boolean ConnectedToRemoteHost = false;
    while (ConnectedToRemoteHost == false){
    try{
    //THE NEXT LINE ALWAYS RETURNS FALSE, UNLESS IT
    SUCCEEDS
    //ON THE FIRST TRY!
    ConnectedToRemoteHost =
    ClientSocketChannel.finishConnect();
    }catch (IOException e){
    System.out.println("client failed to connect");
    }
    }
     
    , Apr 7, 2006
    #1
    1. Advertising

  2. Oliver Wong Guest

    <> wrote in message
    news:...
    >I have a client that is talking to a server. I want to use nonblocking
    > sockets. The client connects fine, so long as the server is already
    > up. But the client doesn't connect if the server isn't already up (of
    > course). Assuming that the server isn't up, I want the client to wait
    > a few seconds and try again. But for some reason, in my code, once the
    > client has failed to connect once, further attempts always fail. In
    > other words, in the code below, finishConnect() should return true once
    > a connection has been established. If the server is already up, it
    > does return true, and everything works. If the server is down,
    > finishConnect() returns false (as it should). But then subsequent
    > calls to it always return false, so the connection is never
    > established. Any idea what I'm doing wrong? What is the proper way to
    > use this?
    >
    > Thanks!
    >
    > //Create a socket to connect to teh server
    > InetSocketAddress InternetSocketAddress = new
    > InetSocketAddress(ServerAddress, Port);
    >
    > SocketChannel ClientSocketChannel = null;
    > try{
    > ClientSocketChannel = SocketChannel.open();
    > ClientSocketChannel.configureBlocking(false);
    > }catch (IOException Exception) {
    > System.out.println("Could not open SocketChannel on
    > client");
    > System.exit(-1);
    > }
    >
    > try{
    > ClientSocketChannel.connect(InternetSocketAddress);
    > }catch (IOException Exception) {
    > System.out.println("Could not connect to server");
    > }
    > Boolean ConnectedToRemoteHost = false;
    > while (ConnectedToRemoteHost == false){
    > try{
    > //THE NEXT LINE ALWAYS RETURNS FALSE, UNLESS IT
    > SUCCEEDS
    > //ON THE FIRST TRY!
    > ConnectedToRemoteHost =
    > ClientSocketChannel.finishConnect();
    > }catch (IOException e){
    > System.out.println("client failed to connect");
    > }
    > }
    >


    Are sure sure that line always returns false? Are you sure the line
    doesn't actually complete because an exception is being thrown?

    - Oliver
     
    Oliver Wong, Apr 7, 2006
    #2
    1. Advertising

  3. On 7 Apr 2006 06:31:01 -0700, wrote:
    > If the server is down, finishConnect() returns false (as it should).
    > But then subsequent calls to it always return false, so the
    > connection is never established.


    Instead of calling finishConnect() repeatedly, I'd recommend using a
    Selector to tell you when it's time to call finishConnect().

    /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, Apr 8, 2006
    #3
    1. Advertising

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. KimTaehwan

    setSoTimeout in nonblocking mode?

    KimTaehwan, Nov 16, 2003, in forum: Java
    Replies:
    0
    Views:
    1,153
    KimTaehwan
    Nov 16, 2003
  2. Peter Ammon

    nonblocking read()

    Peter Ammon, Nov 16, 2004, in forum: Python
    Replies:
    3
    Views:
    587
    Donn Cave
    Nov 17, 2004
  3. anki
    Replies:
    3
    Views:
    332
  4. James Edward Gray II

    Nonblocking Sockets

    James Edward Gray II, Jul 16, 2005, in forum: Ruby
    Replies:
    14
    Views:
    589
    Tanaka Akira
    Jul 18, 2005
  5. rakaur
    Replies:
    4
    Views:
    664
    Bill Kelly
    Feb 14, 2006
Loading...

Share This Page