重点注意:有说明的地方。
package nio.chat;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public class Server extends Thread {
private Selector selector;
private ServerSocketChannel serverChannel;
private ByteBuffer wBuffer=ByteBuffer.allocate(1024); //共享 buffer 在多 链接时候会产生错误
private ByteBuffer rBuffer=ByteBuffer.allocate(1024);
public Server(ServerSocketChannel sc) throws Exception {
serverChannel=sc;
selector = Selector.open();
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
}
public void run(){
while (true){
try{
if (selector.select(1000) == 0){
System.out.print(".");
continue;
}
Iterator<SelectionKey> ki = selector.selectedKeys().iterator();
while (ki.hasNext()){
SelectionKey key = ki.next();
if (key.isAcceptable()){
handleAccept(key);
}else
if (key.isReadable()){
handleRead(key);
}else
if (key.isWritable()){
handleWrite(key);
}
ki.remove();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
private void handleWrite(SelectionKey key){
SocketChannel cc = (SocketChannel) key.channel();
ByteBuffer buf= (ByteBuffer)(key.attachment());
//byte []b={5,'H','E','L','L','O'};
//buf=ByteBuffer.wrap(b);
try{
buf.flip();
cc.write(buf);
if(buf.hasRemaining()==false){
// key.interestOps(SelectionKey.OP_READ);
byte []b=buf.array();
System.out.println("---->response finished!,msg="+new String(b,1,b[0],"utf-8"));
buf.clear();
SelectionKey s=cc.register(selector, SelectionKey.OP_READ, rBuffer);
System.out.println("---toRead,selectionKey="+s);
}
}catch(Exception e){
try{
cc.close();
}catch(Exception e1){};
e.printStackTrace();
System.out.println("---closed!---");
}
}
private void handleRead(SelectionKey key){
SocketChannel cc = (SocketChannel) key.channel();
ByteBuffer buf= (ByteBuffer)(key.attachment());
try{
int read=cc.read(buf);
byte []bs=buf.array();
int pos=buf.position();
if(pos>0 && pos==(int)bs[0]+1){
String msg=new String(bs,1,bs[0],"utf-8");
System.out.println("---->Recv:"+msg);
buf.clear();
String msg1="收到"+(pos-1)+"bytes,content("+msg+")";
bs=msg1.getBytes("utf-8");
byte []bl=new byte[bs.length+1];
bl[0]=(byte)bs.length;
System.arraycopy(bs, 0, bl, 1, bs.length);
wBuffer.put(bl);
//key.interestOps(SelectionKey.OP_WRITE);
SelectionKey s=cc.register(selector, SelectionKey.OP_WRITE, wBuffer);
System.out.println("---toWrite, selectionKey="+s);
}
if(read==-1){
System.out.println("--遇到结束--");
}
}catch(Exception e){
try{
cc.close();
}catch(Exception e1){};
e.printStackTrace();
System.out.println("---closed!---");
}
}
private void handleAccept(SelectionKey key) {
try{
SocketChannel cc = ((ServerSocketChannel) key.channel()).accept();
cc.configureBlocking(false);
SelectionKey s=cc.register(selector, SelectionKey.OP_READ, rBuffer);
System.out.println("---toRead, selectionKey="+s);
//key.interestOps(SelectionKey.OP_READ);
System.out.println("--Connect from "+cc.getRemoteAddress());
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String []args) throws Exception {
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.socket().bind(new InetSocketAddress(Client.port));
serverChannel.configureBlocking(false);
Server sc=new Server(serverChannel);
sc.start();
}
}
package nio.chat;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public class Client extends Thread{
private Selector selector;
private SocketChannel channel;
private BufferedReader br = null;
private ByteBuffer readBuffer=ByteBuffer.allocate(1024);
public Client(SocketChannel c) throws Exception {
this.channel=c;
selector=Selector.open();
channel.register(selector, SelectionKey.OP_WRITE|SelectionKey.OP_READ,null);
}
public void run() {
SelectionKey key;
boolean ced=false;
int loop=0;
while(true){
try{
if(channel.finishConnect() && ced==false){
ced=true;
}
if(ced==false){
continue;
}
int is=selector.select(); //先获取就绪数,如不调用本方法, 这 selectedKeys() 得到不就绪的 SelectionKey
if(is==0){
continue;
}
Iterator<SelectionKey> ks = selector.selectedKeys().iterator();
while (ks.hasNext()){
key = ks.next();
if (key.isWritable() ){
send();
}
if(key.isReadable()){
read();
}
ks.remove(); //从 selector 删除, 如不删除,会导致下次 selectedKeys() 无法加入就绪的 SelectionKey
}
loop++;
}catch(Exception e){
e.printStackTrace();
break;
}
}
System.out.println("---end of chat-----");
try{
channel.close();
}catch(Exception e){
e.printStackTrace();
}
}
private void read() throws Exception {
ByteBuffer bu=ByteBuffer.allocate(20);
int read=channel.read(bu);
for(int i=0;i<read;i++){
readBuffer.put(bu.get(i));
int len=readBuffer.get(0);
if(len==readBuffer.position()-1){
byte []bs=readBuffer.array();
System.out.println("\r\n--收到:"+new String(bs,1,len,"utf-8"));
readBuffer.clear();
}
}
}
static int nLine=0;
static long lastTime=0;
private void send(){
nLine++;
if(System.currentTimeMillis()-lastTime<10*1000){
return;
}
lastTime=System.currentTimeMillis();
try{
String line=String.valueOf(nLine);
byte []bs=line.getBytes("utf-8");
int len=bs.length;
byte []b={(byte)len};
ByteBuffer b1=ByteBuffer.wrap(b);
ByteBuffer b2=ByteBuffer.wrap(bs);
while(b1.hasRemaining()){
channel.write(b1);
}
while(b2.hasRemaining()){
channel.write(b2);
}
System.out.println("\t 发送完毕!,msg="+line);
}catch(Exception e){
e.printStackTrace();
System.exit(0);
}
}
public static String host="127.0.0.1";
public static int port=8001;
public static void main(String []args) throws Exception {
SocketChannel clntChan = SocketChannel.open();
clntChan.configureBlocking(false);
Client dealer=new Client(clntChan);
clntChan.connect(new InetSocketAddress(host, port));
dealer.start();
}
}
相关推荐
赠送jar包:xnio-nio-3.8.0.Final.jar; 赠送原API文档:xnio-nio-3.8.0.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.0.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.0.Final.pom; 包含翻译后的API...
Java-NIO非阻塞服务器示例 本资源主要讲解了Java-NIO非阻塞服务器的示例,通过使用Java-NIO包来实现非阻塞的服务器端模式。下面是从代码中提取的知识点: 一、Java-NIO包简介 Java-NIO(New I/O)包是Java 1.4...
赠送jar包:httpcore-nio-4.4.6.jar 赠送原API文档:httpcore-nio-4.4.6-javadoc.jar 赠送源代码:httpcore-nio-4.4.6-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.6-javadoc-API文档-中文(简体)版.zip ...
本示例"JAVA-NIO-DEMO"提供了关于Java NIO的实际应用,通过Anontion(注解)、Applet(小程序)和NIO的Demo,帮助开发者更深入地理解和掌握这些概念。 首先,让我们深入了解Java NIO。NIO的核心组件包括: 1. **...
Java NIO (Non-blocking Input/Output) 是Java平台中用于高效处理I/O操作的一种机制,它与传统的IO模型( Blocking I/O)相比,提供了更高级别的抽象,允许应用程序以非阻塞的方式读写数据,提高了并发性能。...
赠送jar包:httpcore-nio-4.4.15.jar 赠送原API文档:httpcore-nio-4.4.15-javadoc.jar 赠送源代码:httpcore-nio-4.4.15-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.15-javadoc-API文档-中文(简体)版....
赠送jar包:httpcore-nio-4.4.10.jar; 赠送原API文档:httpcore-nio-4.4.10-javadoc.jar; 赠送源代码:httpcore-nio-4.4.10-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.10.pom; 包含翻译后的API文档...
基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络框架 xSocket.zip 基于java的开发源码-NIO网络...
赠送jar包:xnio-nio-3.8.4.Final.jar; 赠送原API文档:xnio-nio-3.8.4.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.4.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.4.Final.pom; 包含翻译后的API...
赠送jar包:httpcore-nio-4.4.15.jar 赠送原API文档:httpcore-nio-4.4.15-javadoc.jar 赠送源代码:httpcore-nio-4.4.15-sources.jar 包含翻译后的API文档:httpcore-nio-4.4.15-javadoc-API文档-中文(简体)-...
赠送jar包:httpcore-nio-4.4.10.jar; 赠送原API文档:httpcore-nio-4.4.10-javadoc.jar; 赠送源代码:httpcore-nio-4.4.10-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.10.pom; 包含翻译后的API文档...
《深入解析httpcore-nio-4.3.jar:构建高性能的Java非阻塞网络通信》 在Java网络编程中,高效、稳定且可扩展的通信框架至关重要。Apache HttpComponents项目中的HttpCore NIO模块(httpcore-nio)就是这样一个框架...
Java NIO,全称为New Input/Output,是Java在1.4版本引入的一个新特性,是对传统的I/O模型的一种改进。传统的Java I/O基于字节流和字符流,而NIO则提供了通道(Channels)和缓冲区(Buffers)的概念,以及非阻塞I/O...
赠送jar包:httpcore-nio-4.4.5.jar; 赠送原API文档:httpcore-nio-4.4.5-javadoc.jar; 赠送源代码:httpcore-nio-4.4.5-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.5.pom; 包含翻译后的API文档:...
赠送jar包:xnio-nio-3.8.4.Final.jar; 赠送原API文档:xnio-nio-3.8.4.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.4.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.4.Final.pom; 包含翻译后的API...
赠送jar包:xnio-nio-3.8.0.Final.jar; 赠送原API文档:xnio-nio-3.8.0.Final-javadoc.jar; 赠送源代码:xnio-nio-3.8.0.Final-sources.jar; 赠送Maven依赖信息文件:xnio-nio-3.8.0.Final.pom; 包含翻译后的API...
赠送jar包:httpcore-nio-4.4.6.jar; 赠送原API文档:httpcore-nio-4.4.6-javadoc.jar; 赠送源代码:httpcore-nio-4.4.6-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.6.pom; 包含翻译后的API文档:...
赠送jar包:httpcore-nio-4.4.14.jar; 赠送原API文档:httpcore-nio-4.4.14-javadoc.jar; 赠送源代码:httpcore-nio-4.4.14-sources.jar; 赠送Maven依赖信息文件:httpcore-nio-4.4.14.pom; 包含翻译后的API文档...
Java NIO(Non-blocking Input/Output)是一种在Java中处理I/O操作的新方式,相比于传统的BIO( Blocking I/O),NIO提供...通过理解和实践"java-NIO-demo",你将能够更好地掌握NIO的精髓,并在实际项目中发挥其优势。