Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

selector.select() problem

 
Adnan Badar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
i am getting problem in my chat server. It starts smooth but after some time
int numKeys=selector.select();
numKeys value always arrives 0 causes loop to traverse infinitely. Can anyone please help me to figure it out. Thanks in advance.
Here is my code.

public void run() {

//Adnan 6-8-2003
StringBuffer stringBuffer = null;
ByteBuffer byteBuffer= null;
try {//try0
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false); //Non Blocking mode
serverSocket = serverSocketChannel.socket();
InetSocketAddress isa = new InetSocketAddress(serverport);
serverSocket.bind(isa);
selector = Selector.open();
serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);

byteBuffer = ByteBuffer.allocate(4096);

while(!shutdown){
//Auth Server will live until user stops it

try{
int numKeys=selector.select();
//after some time this value always arrives 0
System.out.println("numKeys= "+numKeys);
if(numKeys==0)
continue;

if(numKeys >0)
{ //if1
Set skeys = selector.selectedKeys();//get Selection set related to this event.
Iterator iterator=skeys.iterator();

if(iterator!=null)
while(iterator.hasNext()) { //while1
iterationsCount++;
SelectionKey selectionKey = (SelectionKey)iterator.next();
iterator.remove();
try{ //trywhile1
int rskOps = selectionKey.readyOps();//Get the type of operation
if ((rskOps & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {

Socket socket=serverSocket.accept();
isocket=socket;//for check only
SocketChannel socketChannel = socket.getChannel();
socketChannel.configureBlocking(false);
socketChannel.register(selector,SelectionKey.OP_READ);
}
else
if ((rskOps & SelectionKey.OP_READ) == SelectionKey.OP_READ){
SocketChannel socketChannel = (SocketChannel)selectionKey.channel();
try {
if ( socketChannel == null)
throw new AssertionError("Couldn't get the socket Channel");
byteBuffer.clear();

count = socketChannel.read(byteBuffer);

if(stringBuffer==null)
stringBuffer = new StringBuffer(1);
byteBuffer.flip();
//my utility mrthod for conversion
protocolUtil.appendByteBufferToStringBuffer(byteBuffer,stringBuffer);
byteBuffer.clear();

}catch (Exception ex) {
socketChannel.close();
socketClose(selectionKey,socketChannel.socket()) ;
}
if (byteBuffer.limit() == 0 || (count ==-1) )
{
socketChannel.close();
socketClose(selectionKey,socketChannel.socket());
break;
}
else{

if(stringBuffer!=null && stringBuffer.toString().length()>0)
processRequest(stringBuffer.toString());
}
}
//cleanup
if(stringBuffer!=null){
stringBuffer.delete(0,stringBuffer.length());
stringBuffer = null;
}
}catch(Exception e){//end trywhile1
System.out.println("Auth Server, Exception end trywhile1");
e.printStackTrace();
((SocketChannel)selectionKey.channel()).close();
socketClose(selectionKey, ((SocketChannel)selectionKey.channel()).socket() );
}
} //end while1
} //end if1
}//end try1
catch(Exception ee)
{
ee.printStackTrace();
}
} //end while0 shutdown
}//end try0
catch(Exception ex){

ex.printStackTrace();
}
finally{
//cleanup
if(stringBuffer!=null)
stringBuffer.delete(0,stringBuffer.length() );
byteBuffer = null;
stringBuffer = null;
}

}

private void socketClose(SelectionKey sk,Socket socket) {
try {
synchronized (sk) {
sk.cancel();
}
socket.close();
socket=null;
}catch (Exception ex) {
}
catch(Throwable t) {
}
}
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i hope that you got your solution by now...

Maki Jav
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic