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

    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.socket().bind(new, Port));
    selector =;
    server.register(selector, SelectionKey.OP_ACCEPT);

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

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

    Here's the client :

    Selector selector =;
    SocketChannel client =;
    while (!client.finishConnect())

    SelectionKey clientKey = client.register(selector, SelectionKey.OP_READ);

    The client does its bussiness and gracefully quits.

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

    Thank you,

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


    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
    Rep Power


    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
    Rep Power


    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, 09: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, 11:09 PM
  4. Problem in java
    By saytri in forum New To Java
    Replies: 6
    Last Post: 01-09-2008, 05: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