Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: NIO Headaches

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default NIO Headaches

    I'm having problems with some NIO test code....

    I'm attempting to use a selector to choose a readable channel. The issue is, however, that the channel gets selected but reads no data. I'm probably missing something....

    [CODE]
    package bg.testing.network;
     
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.nio.ByteBuffer;
    import java.nio.channels.SelectionKey;
    import java.nio.channels.Selector;
    import java.nio.channels.ServerSocketChannel;
    import java.nio.channels.SocketChannel;
    import java.util.Iterator;
    import java.util.Set;
     
     
    public class SelectorTest {
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		new Thread(new Server()).start();
    		new Thread(new Client()).start();
    	}
     
    	static class Server implements Runnable {
     
    		Selector sel;
     
    		@Override
    		public void run() {
    			try {
    				ServerSocketChannel server = ServerSocketChannel.open();
    				server.socket().bind(new InetSocketAddress(5555));
    				server.configureBlocking(false);
    				sel = Selector.open();
    				server.register(sel, SelectionKey.OP_ACCEPT);
     
    				boolean running = true;
    				while(running) {
    					int count = sel.select();
    					if(sel.isOpen() && count > 0) {
    						Set<SelectionKey> keyset = sel.selectedKeys();
    						synchronized(keyset) {
    							Iterator<SelectionKey> i = keyset.iterator();
    							while(i.hasNext()) {
    								SelectionKey key = i.next();
    								i.remove();
    								processKey(key);
    							}
    						}
    					} else if(!sel.isOpen())
    						running = false;
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
     
    		}
     
    		private void processKey(SelectionKey key) {
     
    			if(key.isValid() && key.isAcceptable()) {
    				try {
    					SocketChannel chan = ((ServerSocketChannel)key.channel()).accept();
    					chan.configureBlocking(false);
    					chan.register(sel, SelectionKey.OP_READ);
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
     
    			if(key.isValid() && key.isReadable()) {
    				System.out.println("Read starting...");
    				SocketChannel chan = (SocketChannel) key.channel();
    				ByteBuffer buff = ByteBuffer.allocate(1024);
    				try {
    					while((chan.read(buff))>=0) {
    						buff.flip();
    						System.out.println("read some");
    						buff.clear();
    					}
    					chan.close();
    					System.out.println("Read complete");
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			}
    		}
     
    	}
     
    	static class Client implements Runnable {
     
    		@Override
    		public void run() {
    			try {
    				SocketChannel chan = SocketChannel.open();
    				chan.connect(new InetSocketAddress("localhost", 5555));
    				while(!chan.finishConnect());
    				ByteBuffer buff = ByteBuffer.allocate(1024);
    				for(int i=0;i<1000;i++) {
    					buff.flip();
    					chan.write(buff);
    					buff.compact();
    				}
    				chan.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
     
    		}
     
    	}
    }
     
    [/CODE]

    The "read some" statement is never printed.
    Last edited by bgroenks96; February 16th, 2012 at 11:52 PM. Reason: added info


Tags for this Thread