Note that there are some explanatory texts on larger screens.

plurals
  1. PONIO Server not able to listen to client
    primarykey
    data
    text
    <p>Hi I am trying to implements a simple Java NIO server; which registers the socketChannel with the selector. Hence I wish to listen to client and send some response back. After the socketChannel is registered with the selector, even if client(non NIO) sends some data, Server is not able to read; howerver the key generated is still being iterated.</p> <p>Detailed View: Server Side: </p> <pre><code>**First thread**: </code></pre> <p>public void run() { while (true) {</p> <pre><code> ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.configureBlocking(true); serverSocketChannel.socket().bind(inetAdressOfServer); SocketChannel clientChannel = serverSocketChannel.accept(); new Listener("").addSocketChannel(clientChannel); }} **Second Thread**: static Selector selector = Selector.open(); public boolean addSocketChannel(SocketChannel clientChannel) { SelectionKey key = clientSocketChannel.register(selector, selector.OP_READ|SelectionKey.OP_WRITE); key.attach(new ChannelCallback(clientSocketChannel)); return key.isValid(); } public void run() { Set keysSet = selector.keys(); Iterator i = keysSet.iterator(); while (i.hasNext()) { SelectionKey key = (SelectionKey) i.next(); } if (key.isReadable()) { //read and do something } } Client Side: Socket socket = new Socket(serverIP, serverPort); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); dos.writeBytes(str + "\n"); </code></pre> <p>NB: When done in single thread, the same program works, but when implemented in the manner mentioned above causes it not to listen to client. Please help me to resolve this issue.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload