Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have read tons of documentation, forums and, obviously each of your responses. I have developed and tested a solution. It is not the best, but it is my best approximation.</p> <p>I use <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/channels/Selector.html#selectNow%28%29" rel="nofollow">selectNow()</a> method of <a href="http://docs.oracle.com/javase/7/docs/api/index.html?java/nio/channels/Selector.html" rel="nofollow">Selector</a>. Given a <code>Selector</code> named <code>selector</code> that have a <a href="http://docs.oracle.com/javase/7/docs/api/index.html?java/nio/channels/SocketChannel.html" rel="nofollow">SocketChannel</a> registered, I can use this method to know if the associated <a href="http://docs.oracle.com/javase/7/docs/api/java/net/Socket.html" rel="nofollow">Socket</a> has readable bytes:</p> <pre><code>public boolean isReadable() { try { selector.selectNow(); Set&lt;SelectionKey&gt; selectedKeys = selector.selectedKeys(); Iterator&lt;SelectionKey&gt; keyIterator = selectedKeys.iterator(); while(keyIterator.hasNext()) { SelectionKey selectionKey = keyIterator.next(); if(!selectionKey.isValid()) { continue; } if(selectionKey.isReadable()) { return true; } keyIterator.remove(); } } catch(IOException e) { // error } return false; } </code></pre> <p>It is the only way I have found to know if a Socket has readable bytes without reading the bytes. Do you know a better way to do it or disadvantages of doing it by this way?</p> <p><strong>EDIT:</strong></p> <p>This solution works the first time, but it has a drawback: it always returns true even if 0 bytes are readable. A socket can be readable even if no data is available. This is not the behaviour I want, because I don't want to read when there is no data in the read buffer.</p> <p><strong>EDIT2 BIS:</strong></p> <p>Test this code:</p> <pre><code>public static boolean isReadable(Selector selector) { try { selector.selectNow(); Set&lt;SelectionKey&gt; selectedKeys = selector.selectedKeys(); Iterator&lt;SelectionKey&gt; keyIterator = selectedKeys.iterator(); while (keyIterator.hasNext()) { SelectionKey selectionKey = keyIterator.next(); if (!selectionKey.isValid()) { continue; } if (selectionKey.isReadable()) { keyIterator.remove(); return true; } keyIterator.remove(); } } catch (IOException e) { System.err.println("Error!"); e.printStackTrace(); } return false; } public static void read() { try { Selector selector = Selector.open(); SocketChannel socketChannel = SocketChannel.open(); Socket socket = socketChannel.socket(); System.out.println("Connecting to server..."); socket.connect(new InetSocketAddress("localhost", 9999)); System.out.println("Connected to server."); socketChannel.configureBlocking(false); socketChannel.register(selector, SelectionKey.OP_READ); ByteBuffer bb = ByteBuffer.allocate(1024); while (true) { boolean readable = isReadable(selector); if (readable) { System.out.println("Readable data found! Let's read..."); bb.clear(); int readBytes = socketChannel.read(bb); if (readBytes &lt; 0) { System.out.println("End of Stream found"); break; } byte[] readArray = new byte[readBytes]; System.arraycopy(bb.array(), 0, readArray, 0, readBytes); System.out.println("Read (" + (readBytes) + " bytes): " + new String(readArray, Charset.forName("UTF-8"))); bb.flip(); socketChannel.write(bb); Thread.sleep(1000); } else { System.out.println("No data to read, sleeping"); Thread.sleep(1000); } } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { read(); } </code></pre> <p>First of all, I execute an application that listens for connections and writes data: I used <a href="http://netcat.sourceforge.net/" rel="nofollow">netcat</a> with this command: <code>nc -l 9999</code>. Then I execute my Java program. Finally I can write text lines in <code>netcat</code> and my Java program works perfectly.</p> <p>Remember it is a proof of concept, not a way to real programming solution. In fact, it is a very bad practise. Thank you @EJP and @Markus!</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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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