//1.获取通道
SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8999));
//2.将通道改成非阻塞式的
sChannel.configureBlocking(false);
//3.开启指定大小的缓冲区
ByteBuffer bBuf = ByteBuffer.allocate(1024);
//简单的聊天室
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
String str = scanner.next();
//4.写入一些数据
bBuf.put((new Date().toString()+"\n"+str).getBytes());
//5.将数据写入通道
//切换成读模式
bBuf.flip();
//写入
sChannel.write(bBuf);
bBuf.clear();
}
//关闭通道
sChannel.close();