need a nio client app

Discussion in 'Java' started by harryos, Jul 3, 2008.

  1. harryos

    harryos Guest

    hi friends
    i am learning java.nio from ibm alphawks tutorial that has a
    Multiportecho application .I tested it using ordinary io socket client
    that reads keyboard input and sends to server .But i am unable to
    figure out how to do the same using nio classes.Can anybody give me a
    nio client app so i can learn how it works?

    thanks
    harryos, Jul 3, 2008
    #1
    1. Advertising

  2. harryos

    Arne Vajhøj Guest

    harryos wrote:
    > i am learning java.nio from ibm alphawks tutorial that has a
    > Multiportecho application .I tested it using ordinary io socket client
    > that reads keyboard input and sends to server .But i am unable to
    > figure out how to do the same using nio classes.Can anybody give me a
    > nio client app so i can learn how it works?


    You connect many clients to one server. The server has many
    connections - each client has only one connection.

    NIO can be useful at the sever, but there are no need to use
    it on the client.

    Arne
    Arne Vajhøj, Jul 3, 2008
    #2
    1. Advertising

  3. harryos

    harryos Guest


    >
    > NIO can be useful at the sever, but there are no need to use
    > it on the client.
    >


    i know that..still i am trying to learn how to use nio to do this.
    I am stuck with the part doing keyboard input and selector doing
    select.I can't figure out how to bring in both these in code. if
    someone can give a code example it wd be helpful
    thanks
    H
    harryos, Jul 3, 2008
    #3
  4. harryos

    Neil Coffey Guest

    harryos wrote:

    > i am learning java.nio from ibm alphawks tutorial that has a
    > Multiportecho application .I tested it using ordinary io socket client
    > that reads keyboard input and sends to server .But i am unable to
    > figure out how to do the same using nio classes.Can anybody give me a
    > nio client app so i can learn how it works?


    Using NIO from a client is not much different than from a server. The
    steps are more or less as follows:

    (1) You'll basically have two threads: one reading your keyboard input
    and another handling the NIO stuff (the selector loop);
    (2) Create some object that will wrap around the data that is waiting
    to be sent down the socket at any given time;
    (3) Connect using a socket channel:

    SocketChannel sc = SocketChannel.open();
    sc.configureBlocking(false);
    InetSocketAddress socketAddress =
    new InetSocketAddress(hostAddr, port);
    // this next object is what your keyboard thread will
    // post pending output to
    DataWrapper d = new DataWrapper();
    sc.register(selector, SelectionKey.OP_CONNECT, d);
    sc.connect(socketAddress);

    (4) Your selector thread looks something like this:

    while (!stopRequest) {
    if (selector.select() == 0) continue;
    for (Iterator it = selector.selectedKeys().iterator();
    it.hasNext();) {
    SelectionKey key = (SelectionKey) it.next();
    it.remove();
    if (!key.isValid()) continue;
    DataWrapper data = (DataWrapper) key.attachment();
    SocketChannel sc = (SocketChannel) key.channel();

    try {
    if (key.isConnectable()) {
    if (!sc.finishConnect()) {
    // close sc and signal problem to kb thread
    } else {
    // signal to kb thread that it can send data
    }
    }
    if (key.isWritable()) {
    synchronized (data) {
    // send data from 'data' down the
    // sc socket channel; if all data in the buffer is now
    // sent, de-register for writes
    }
    }
    } catch (Exception e) {
    ...
    }
    }
    }

    (5) Then, your kb thread initially must wait for the signal that
    connection has completed, then waits for input. When input occurs,
    it synchronizes on the DataWrapper, adds data to it, and then
    registers the socket for writes, and wakes up the selector.
    [IIRC, in Java 1.4 there's a threading issue that means you
    have to register for writes AFTER the selector.select() wakes
    up-- i.e. you add the socket to some kind of list that you then
    check for after selector.select() and register each channel in the
    list for writes.]

    Hope all that makes sense. The skeleton above is adapted from some
    working code I wrote a while ago, bar any copy-and-pastos. Note that
    in my case, I was connecting to multiple servers from a single client.
    It's not actually that common to use NIO on a client, and from
    the description of your requirements (connecting to a single server?)
    I'm not sure you'll see any benefit.

    Neil
    Neil Coffey, Jul 6, 2008
    #4
  5. harryos

    Naveen Kumar Guest

    On Jul 6, 9:30 am, Neil Coffey <>
    wrote:
    > harryos wrote:
    > > i am learning java.nio from ibm alphawks tutorial that has a
    > > Multiportecho application .I tested it using ordinary io socket client
    > > that reads keyboard input and sends to server .But i am unable to
    > > figure out how to do the same using nio classes.Can anybody give me a
    > > nio client app so i can learn how it works?

    >
    > Using NIO from a client is not much different than from a server. The
    > steps are more or less as follows:
    >
    > (1) You'll basically have two threads: one reading your keyboard input
    > and another handling the NIO stuff (the selector loop);
    > (2) Create some object that will wrap around the data that is waiting
    > to be sent down the socket at any given time;
    > (3) Connect using a socket channel:
    >
    > SocketChannel sc = SocketChannel.open();
    > sc.configureBlocking(false);
    > InetSocketAddress socketAddress =
    > new InetSocketAddress(hostAddr, port);
    > // this next object is what your keyboard thread will
    > // post pending output to
    > DataWrapper d = new DataWrapper();
    > sc.register(selector, SelectionKey.OP_CONNECT, d);
    > sc.connect(socketAddress);
    >
    > (4) Your selector thread looks something like this:
    >
    > while (!stopRequest) {
    > if (selector.select() == 0) continue;
    > for (Iterator it = selector.selectedKeys().iterator();
    > it.hasNext();) {
    > SelectionKey key = (SelectionKey) it.next();
    > it.remove();
    > if (!key.isValid()) continue;
    > DataWrapper data = (DataWrapper) key.attachment();
    > SocketChannel sc = (SocketChannel) key.channel();
    >
    > try {
    > if (key.isConnectable()) {
    > if (!sc.finishConnect()) {
    > // close sc and signal problem to kb thread
    > } else {
    > // signal to kb thread that it can send data
    > }
    > }
    > if (key.isWritable()) {
    > synchronized (data) {
    > // send data from 'data' down the
    > // sc socket channel; if all data in the buffer is now
    > // sent, de-register for writes
    > }
    > }
    > } catch (Exception e) {
    > ...
    > }
    > }
    > }
    >
    > (5) Then, your kb thread initially must wait for the signal that
    > connection has completed, then waits for input. When input occurs,
    > it synchronizes on the DataWrapper, adds data to it, and then
    > registers the socket for writes, and wakes up the selector.
    > [IIRC, in Java 1.4 there's a threading issue that means you
    > have to register for writes AFTER the selector.select() wakes
    > up-- i.e. you add the socket to some kind of list that you then
    > check for after selector.select() and register each channel in the
    > list for writes.]
    >
    > Hope all that makes sense. The skeleton above is adapted from some
    > working code I wrote a while ago, bar any copy-and-pastos. Note that
    > in my case, I was connecting to multiple servers from a single client.
    > It's not actually that common to use NIO on a client, and from
    > the description of your requirements (connecting to a single server?)
    > I'm not sure you'll see any benefit.
    >
    > Neil





    Noted a nice thread on nio client/server on this group...
    http://groups.google.com/group/cjug

    Search and have a look at that. It does has a full fledge sample
    program too. Guess it would be beneficial for u to understand and use
    it.

    Naveen Kumar
    Naveen Kumar, Jul 6, 2008
    #5
    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. Avizz
    Replies:
    3
    Views:
    13,734
    Andy Fish
    Sep 29, 2003
  2. Corey_G

    use java.nio for http client?

    Corey_G, May 7, 2004, in forum: Java
    Replies:
    13
    Views:
    8,298
    Marc Slemko
    May 10, 2004
  3. iksrazal

    NIO with timeouts != NIO?

    iksrazal, Jun 17, 2004, in forum: Java
    Replies:
    1
    Views:
    6,198
    iksrazal
    Jun 18, 2004
  4. A.R.
    Replies:
    0
    Views:
    495
  5. brieuc
    Replies:
    5
    Views:
    2,140
    Esmond Pitt
    Mar 20, 2007
Loading...

Share This Page