JDK1.4.1 Beta NIO Selector select problem. !_NEWLINE_!
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   John_Lee
Posted On:   Monday, July 8, 2002 09:51 AM

OS : Win2k JDK1.4.1 beta Hi! I am running into weird problem with Selector select call. It does not seem to block appropriately. So it goes to infinte loop. You do not see this problem with JDK1.4.0_x version. Is this bug or some kind of behavior change of Selecot select call. My server code is based on following code. It is from following website. I am sorry for linking the website without author's permission. (but it is pretty much in public domain) http://www.owlmountain.com/tutorials/NonBlockingIo.htm#_Toc524339534 You can see same infinite loop with this example. Any comment would be appreciated. Thanks ahead. jong    More>>


OS : Win2k
JDK1.4.1 beta


Hi! I am running into weird problem with Selector select call.
It does not seem to block appropriately. So it goes to infinte loop.
You do not see this problem with JDK1.4.0_x version. Is this bug or some kind of behavior change of Selecot select call.

My server code is based on following code.
It is from following website.
I am sorry for linking the website without author's permission.
(but it is pretty much in public domain)
http://www.owlmountain.com/tutorials/NonBlockingIo.htm#_Toc524339534
You can see same infinite loop with this example.
Any comment would be appreciated.

Thanks ahead.

jong




			
				
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.spi.*;
import java.nio.charset.*;
import java.net.*;
import java.util.*;


public class NonBlockingServer {

int port = 4001;
Selector selector = null;
ServerSocketChannel selectableChannel = null;
int keysAdded = 0;


static String QUIT_SERVER = "quit";
static String SHUTDOWN = "shutdown";

public NonBlockingServer() {
}

public NonBlockingServer( int port ) {
this.port = port;
}

public void initialize()

throws IOException {
this.selector = SelectorProvider.provider().openSelector();
this.selectableChannel = ServerSocketChannel.open();
this.selectableChannel.configureBlocking(false);
InetAddress lh = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(lh, this.port );
this.selectableChannel.socket().bind(isa);
}

public void finalize()

throws IOException {
this.selectableChannel.close();
this.selector.close();
}

public void acceptConnections()
throws IOException, InterruptedException {

SelectionKey acceptKey =
this.selectableChannel.register( this.selector,
SelectionKey.OP_ACCEPT );


while (( this.keysAdded = acceptKey.selector().select()) > 0 ) {


Set readyKeys = this.selector.selectedKeys();
Iterator i = readyKeys.iterator();

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

if ( key.isAcceptable() ) {
ServerSocketChannel nextReady =
(ServerSocketChannel)key.channel();


SocketChannel channel = nextReady.accept();
channel.configureBlocking( false );
SelectionKey readKey =
channel.register( this.selector,
SelectionKey.OP_READ|SelectionKey.OP_WRITE );
readKey.attach( new ChannelCallback( channel ) );
}

else if ( key.isReadable() ) {
SelectableChannel nextReady =(SelectableChannel) key.channel();
this.readMessage( (ChannelCallback) key.attachment() );
}

else if ( key.isWritable() ) {
ChannelCallback callback = (ChannelCallback) key.attachment();
String message = "What is your name? ";
ByteBuffer buf = ByteBuffer.wrap( message.getBytes() );
int nbytes = callback.getChannel().write( buf );
}
}
}


}



public void writeMessage( SocketChannel channel, String message )
throws IOException {
ByteBuffer buf = ByteBuffer.wrap( message.getBytes() );
int nbytes = channel.write( buf );
}



static final int BUFSIZE = 8;

public String decode( ByteBuffer byteBuffer )
throws CharacterCodingException {
Charset charset = Charset.forName( "us-ascii" );
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode( byteBuffer );
String result = charBuffer.toString();
return result;
}

public void readMessage( ChannelCallback callback )
throws IOException, InterruptedException {
ByteBuffer byteBuffer = ByteBuffer.allocate( BUFSIZE );
int nbytes = callback.getChannel().read( byteBuffer );
byteBuffer.flip();
String result = this.decode( byteBuffer );

if ( result.indexOf( "quit" ) >= 0 ) callback.getChannel().close();
else if ( result.indexOf( "shutdown" ) >= 0 ) {
callback.getChannel().close();
throw new InterruptedException();
}
else {
callback.append( result.toString() );
//If we are done with the line then we execute the callback.
if ( result.indexOf( "
" ) >= 0 )
callback.execute();
}
}

public class ChannelCallback {
private SocketChannel channel;
private StringBuffer buffer;

public ChannelCallback( SocketChannel channel ) {
this.channel = channel;
this.buffer = new StringBuffer();
}

public void execute() throws IOException {

writeMessage( this.channel, this.buffer.toString() );
buffer = new StringBuffer();
}

public SocketChannel getChannel() {
return this.channel;
}

public void append( String values ) {
buffer.append( values );
}

}



public static void main( String[] args ) {


NonBlockingServer nbServer = new NonBlockingServer();

try {
nbServer.initialize();
} catch ( IOException e ) {
e.printStackTrace();
System.exit( -1 );
}

try {
nbServer.acceptConnections();
}

catch ( IOException e ) {
e.printStackTrace();

}

catch ( InterruptedException e ) {

}

}

}





			
				
here is generic client you can use as test client.

java ChatClient [hostname] [portnumber]




import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class ChatClient extends Panel implements Runnable
{

private TextField tf = new TextField();
private TextArea ta = new TextArea();

private Socket socket;

private OutputStream out;
private InputStream in;


public ChatClient( String host, int port ) {

setLayout( new BorderLayout() );
add( "North", tf );
add( "Center", ta );

tf.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
processMessage( e.getActionCommand() );
}
} );


try {


socket = new Socket( host, port );

System.out.println( "connected to "+socket );

in = socket.getInputStream();
out = socket.getOutputStream();


new Thread( this ).start();
} catch( IOException ie ) { System.out.println( ie ); }
}

private void processMessage( String message ) {
try {
out.write( message.getBytes() );
out.write("
".getBytes());
tf.setText( "" );
} catch( IOException ie ) { System.out.println( ie ); }
}


public void run() {
try {

byte buffer[] = new byte[4096];


while (true) {

int r = in.read( buffer );
String message = new String( buffer, 0, r );


ta.append( message);
}
} catch( IOException ie ) { System.out.println( ie ); }
}

public static void main(String [] args){

String hostName=args[0];
String portNum=args[1];
int portN=Integer.parseInt(portNum);
Frame frame=new Frame();
frame.add(new ChatClient(hostName,portN));
frame.setSize(400,200);
frame.setVisible(true);

}
}



   <<Less
About | Sitemap | Contact