java NIO GNU实现,read的时候如果throw exception的不通

 
client = (SocketChannel) selectionKey.channel();
if(client.isConnected())
System.out.println("connect ok");
else
{
System.out.println("connect NOT ok");
selectionKey.cancel();
try {
selectionKey.channel().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//将缓冲区清空以备下次读取
receivebuffer.clear();
//读取服务器发送来的数据到缓冲区中
try {
count = client.read(receivebuffer);
} catch (IOException e) {
System.out.println("Exception");
// TODO Auto-generated catch block
selectionKey.cancel();
try {
selectionKey.channel().close();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
 
 
在x86上,如果客户端按CTRL+C终止,则会在下一个Exception
在Mips上,如果客户端按CTRL+C终止,则会在上一个Exception
 
 
Exception in thread "main" java.nio.channels.NotYetConnectedException
   at gnu.java.nio.SocketChannelImpl.read(SocketChannelImpl.java:217)
   at net.tsong.javanio.NIOServer.handleKey(NIOServer.java:122)
   at net.tsong.javanio.NIOServer.listen(NIOServer.java:62)
   at net.tsong.javanio.NIOServer.main(NIOServer.java:192)
#
 
 
实现代码:SocketChannelImpl.java:217 
 
 
  public int read(ByteBuffer dst) throws IOException
  {
    if (!isConnected())
      throw new NotYetConnectedException();
   
    return channel.read(dst);
  }
   
 
 
由此可见,GNU的classpath在read之前会判断是否isConnected
SUN java应该没有判断
 

 

没有评论: