Non Blocking SocketChannel and Deregistering Selector keys

Discussion in 'Java' started by RFleming@NationalSteel.com, Jun 19, 2007.

  1. Guest

    Hello,

    I have been working through Java Sockets and Socket channels the last
    few days. I have poured over many examples and referenced Java in a
    nutshell. I am able to successfully use the socketchannel class in
    non blocking mode and to read async as well. For better or for worse
    I decided to create a class that allows me to use up to 100
    socketchannels using a fixed array. I originally had a continuous
    loop monitor the input buffer of all active connections. It worked
    really good. I later read about the selector and decided it seemed
    more efficient.

    As I stated earlier, everything worked really good before the
    selector.

    With the selector, (I am only using one selector to monitor my up to
    100 socketchannels), I am able to successfully register a key for each
    socketchannel in the array, open multiple connections, and read data,
    non blocking, from those same socketchannels. Once I close any
    connection, and later reopen it, the program stops responding. By
    stepping thought the code I see it stops when I try to register a
    socketchannel's key with the selector that was previously registered.
    Meaning socketchannel[0] was opened and registered. Later
    socketchannel[0].close was called. Again later socketchannel[0] was
    opened, and the program hangs on the register command.

    My question is, how can I close a socketchannel and deregister a key
    from the selector without closing the selector itself? I don't want
    to close the selector because other open sockets would lose their
    keys.

    below is a small snippet of the code I am using to set up a
    socketchannel and one way I tried to prevent multiple registrations of
    the selector:

    clientsocketchannel[socketindex] = SocketChannel.open();
    clientsocketchannel[socketindex].configureBlocking(false);
    clientsocketchannel[socketindex].connect(new
    InetSocketAddress(ipaddress, socketport));
    if (clientsocketchannel[socketindex].keyFor(selector) == null) {
    clientsocketchannel[socketindex].register(selector,clientsocketchannel[socketindex].validOps());
    }

    note on the second time opening a particular socket, both the keyfor
    method and isregistered method return null/false, but as soon as the
    program tries to register the socketchannel to the selector for a
    second time, the debug stepper disappears and my jframe controls stop
    responding. I have to press stop in the debugger to quit the program.

    I hope I was successful in explaining my problem and as always I
    greatly appreciate anyone willing to offer suggestions!

    Ryan
    , Jun 19, 2007
    #1
    1. Advertising

  2. Esmond Pitt Guest

    Registering a key with a selector syncrhonizes on the selector. If the
    selector is blocked sleeping so will you.

    You shouldn't be doing that sort of thing from the AWT thread anyway,
    but if you do it in a different thread from the Selector thread you need
    to wakeup the selector before the registration.

    I would also get rid of the array of socket channels and use the
    Selector as the collection of socket channels. Much simpler. You don't
    need an external collection of socket channels at all.
    Esmond Pitt, Jun 20, 2007
    #2
    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. Hendra Gunawan
    Replies:
    1
    Views:
    12,428
    Allan Herriman
    Apr 8, 2004
  2. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    913
    Valentin Tihomirov
    Oct 12, 2003
  3. nukleus
    Replies:
    14
    Views:
    816
    Chris Uppal
    Jan 22, 2007
  4. Christian
    Replies:
    5
    Views:
    723
    Esmond Pitt
    Dec 2, 2007
  5. Serge Savoie
    Replies:
    4
    Views:
    256
    Serge Savoie
    Oct 1, 2008
Loading...

Share This Page