Results 1 to 1 of 1
  1. #1
    bareya is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default Selectors and ServerSocketChannel

    Hi I've problem with Selectors/Channels
    I've found somewhere in web:
    server:
    Java Code:
    byte[] data = new byte[255];
    		for (int i = 0; i < data.length; i++) data[i] = (byte) i;
    		
    		    ServerSocketChannel server = ServerSocketChannel.open();
    		    server.configureBlocking(false);
    		    server.socket().bind(new InetSocketAddress(9000));
    		    Selector selector = Selector.open();
    		    server.register(selector, SelectionKey.OP_ACCEPT);
    		    while (true) {
    		      selector.select();
    		      Set readyKeys = selector.selectedKeys();
    		      Iterator iterator = readyKeys.iterator();
    		      while (iterator.hasNext()) {
    		        SelectionKey key = (SelectionKey) iterator.next();
    		        iterator.remove();
    		        if (key.isAcceptable()) {
    		          SocketChannel client = server.accept();
    		          System.out.println("Accepted connection from " + client);
    		          client.configureBlocking(false);
    		          ByteBuffer source = ByteBuffer.wrap(data);
    		          SelectionKey key2 = client.register(selector, SelectionKey.OP_WRITE);
    		          key2.attach(source);
    		        } else if (key.isWritable()) {
    		          SocketChannel client = (SocketChannel) key.channel();
    		          ByteBuffer output = (ByteBuffer) key.attachment();
    		          if (!output.hasRemaining()) {
    		            output.rewind();
    		          }
    		          client.write(output);
    		        }
    		        key.channel().close();
    		      }
    		    }
    and client:
    Java Code:
    public static void main(String[] args) throws Exception {
    		    int port = 1919;
    		    SocketAddress address = new InetSocketAddress("127.0.0.1", 9000);
    		    SocketChannel client = SocketChannel.open(address);
    		    ByteBuffer buffer = ByteBuffer.allocate(4);
    		    IntBuffer view = buffer.asIntBuffer();
    		    
    		    for (int expected = 0;; expected++) {
    		      client.read(buffer);
    		      int actual = view.get();
    		      buffer.clear();
    		      view.rewind();
    		      if (actual != expected) {
    		        System.err.println("Expected " + expected + "; was " + actual);
    		        break;
    		      }
    		      System.out.println(actual);
    		    }
    		  }
    When I connect to server first time, everything is ok, server is working but when i try connect to server second time client gives me error:
    Java Code:
    Exception in thread "main" java.net.ConnectException: Connection refused: connect
    	at sun.nio.ch.Net.connect(Native Method)
    	at sun.nio.ch.SocketChannelImpl.connect(Unknown Source)
    	at java.nio.channels.SocketChannel.open(Unknown Source)
    	at LClient.main(LClient.java:100)
    my firewall is off, I'm trying to use Selectors, but second conenction to server gives me that error,
    please,
    help
    Peter
    Last edited by bareya; 01-31-2011 at 03:59 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
  •