Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default problem with java nio

    Hi everyone,

    I am having problems with setting up the communication between a server and a client with the java.nio api.
    I am using a selector, a serversocketchannel, and a socketchannel for the client -- so the mechanism is based on SelectionKeys.

    The problem, as it seems to me, is that once the client connects to the server, and the server receives the SelectionKey - acceptable type, and after i invoke remove on the selector's iterator for this specific key, THE SELECTOR.SELECT() METHOD KEEPS RETURNING THE SAME KEY.

    I will give anyone who helps me on this one my entire kingdom !

    Here's some code :

    server

    server = ServerSocketChannel.open();
    server.configureBlocking(false);
    server.socket().bind(new java.net.InetSocketAddress(IP, Port));
    selector = Selector.open();
    server.register(selector, SelectionKey.OP_ACCEPT);

    while (true) {
    n = selector.select();
    Iterator i = selector.selectedKeys().iterator(); while(i.hasNext()) {
    SelectionKey key = (SelectionKey) i.next();
    i.remove();
    log.info("selection [process :::: " + n + " " + key);
    }
    }


    n is always 1 - the key is the same object - the same reference.

    Here's the client :

    Selector selector = Selector.open();
    SocketChannel client = SocketChannel.open();
    client.configureBlocking(false);
    client.connect(new java.net.InetSocketAddress("localhost",8000));
    while (!client.finishConnect())
    ;

    SelectionKey clientKey = client.register(selector, SelectionKey.OP_READ);
    Thread.sleep(10000);
    clientKey.cancel();
    client.close();

    The client does its bussiness and gracefully quits.

    The server hangs on in an infinite loop, and it always finds the same key after selector.select.

    Thank you,
    Andrei

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    When NIO came out, I spent a huge amount of time trying to get it to work. I never made my code work. And lots of people have problems. I think that the NIO design is simply too complex.

    But there is a solution. Check out Apache's MINA.

    Its an easy to use framework that uses NIO and has tons of features.

    Apache MINA - Index

  3. #3
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    Thank you for your reply. I appreciate your help, and under normal circumstances I would have probably followed your advice. But, I have to use this api and not a wrapper. This is because the project is one of my faculty homeworks, so I am doomed to find a solution.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Doomed is right. Start by ignoring the selector portion of the design. Selectors are useful for big servers, but you need to get the underlying ServerSocketChannel and SocketChannel communicating first. In reality, that's enough for most applications.

    As best I understand, the selector functionality allows you to register channels with the selector, and the selector monitors the channels. This would be most useful with multiple channels being handled by a single application. The selector simply tells the application what the latest event is. Once you have the channel logic working, I don't think the Selector will be too bad, just unnecessary.

Similar Threads

  1. JAVA and XML Problem
    By jackchang in forum XML
    Replies: 4
    Last Post: 02-22-2009, 08:28 PM
  2. Java problem
    By grend in forum New To Java
    Replies: 5
    Last Post: 08-18-2008, 11:44 PM
  3. Problem in java
    By saytri in forum New To Java
    Replies: 4
    Last Post: 01-16-2008, 10:09 PM
  4. Problem in java
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-09-2008, 04:13 PM
  5. JAVA if problem
    By toby in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •