🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #1451

How to detect when a SocketChannel is readable?

#1451
From: Yonatan Taub
Date: Tue, 11 Dec 2001 18:38
103 lines
3161 bytes
I've been experimenting recently with jdk1.4 java.nio packages.
I registered a SocketChannel with a selector for SelectionKey.OP_READ.
This "event" never happens.
I managed to do it with SelectionKey.OP_CONNECT but can't with OP_READ.



CLIENT APPLICATION
==================

import java.io.*;
import java.net.*;
import java.util.*;
import java.nio.channels.*;
import java.nio.ByteBuffer;


/**
 *
 */

public class NonBlockingClient {


  /**
   *
   */

  public static void main(String[] args) throws Exception {

    InetSocketAddress sa   = new InetSocketAddress("localhost", 9999);
    SocketChannel channel  = SocketChannel.open();
    Selector selector      = Selector.open();


    channel.configureBlocking(false);
    channel.connect(sa);
    channel.register(selector, SelectionKey.OP_READ);


    while(selector.select() > 0)
    {
      Set readyKeys     = selector.selectedKeys();
      Iterator iterator = readyKeys.iterator();


      while(iterator.hasNext())
      {
        SelectionKey key = (SelectionKey)iterator.next();
        iterator.remove();

        System.out.println("key.isAcceptable(): "   + key.isAcceptable());
        System.out.println("key.isConnectable(): " + key.isConnectable());
        System.out.println("key.isReadable(): "      + key.isReadable());
        System.out.println("key.isValid(): "            + key.isValid());
        System.out.println("key.isWritable(): "       + key.isWritable());
      }
    }

  }

}


SERVER APPLICATION
==================
import java.io.*;
import java.net.*;


/**
 *
 */

public class Server {


  /**
   *
   */

  public static void main(String[] args) throws Exception {
    ServerSocket ss = new ServerSocket(9999);


    while(true)
    {
      System.out.println("Awaiting connection...");
      Socket s = ss.accept();
      System.out.println("Connection accepted: " + s);
      OutputStream os = s.getOutputStream();
      os.write('z');
      os.flush();
    }
  }

}



Am I doing something wrong?
I'm running the application on Windows 2000 Professional with JDK "build
1.4.0-beta-b65" (the first beta).
Technical Details
Message-ID: <3c1652a9_2@dnews>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!supernews.com!newspump.sol.net!newsfeed.cwix.com!newsfeed.frii.net!out.nntp.be!propagator-SanJose!in.nntp.be!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,d816ec485ed2816b,start
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2001-12-11 10:41:03 PST
Reply-To: "Yonatan Taub" <jonathantaub@hotmail.com>
From: "Yonatan Taub" <jonathantaub@hotmail.com>
Newsgroups: borland.public.jbuilder.java.api
Subject: How to detect when a SocketChannel is readable?
Date: Tue, 11 Dec 2001 20:38:31 +0200
Lines: 107
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4807.1700
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700
NNTP-Posting-Host: 212.179.22.2
Message-ID: <3c1652a9_2@dnews>
X-Trace: dnews 1008095913 212.179.22.2 (11 Dec 2001 10:38:33 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!supernews.com!newspump.sol.net!newsfeed.cwix.com!newsfeed.frii.net!out.nntp.be!propagator-SanJose!in.nntp.be!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews