java nio实现非阻塞Socket通信实例

来源:互联网 发布:网络卖警服 编辑:程序博客网 时间:2024/05/23 01:58

服务器

package com.java.xiong.Net17;import java.io.IOException;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.nio.channels.Channel;import java.nio.channels.SelectionKey;import java.nio.channels.Selector;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;import java.nio.charset.Charset;public class NServer {// 用于检测所有的Channel状态的selectorprivate Selector selector = null;static final int PORT = 30000;// 定义实现编码、解码的字符串集对象private Charset charse = Charset.forName("GBK");public void init() throws IOException {selector = Selector.open();// 通过open方法来打开一个未绑定的ServerSocketChannel是咧ServerSocketChannel server = ServerSocketChannel.open();InetSocketAddress isa = new InetSocketAddress("127.0.0.1", PORT);// 将该ServerSocketChannel绑定到指定的IP地址server.bind(isa);// 设置serverSocket已非阻塞方式工作server.configureBlocking(false);// 将server注册到指定的selector对象server.register(selector, SelectionKey.OP_ACCEPT);while (selector.select() > 0) {// 一次处理selector上的每个选择的SelectionKeyfor (SelectionKey sk : selector.selectedKeys()) {// 从selector上已选择的Kye集中删除正在处理的SelectionKeyselector.selectedKeys().remove(sk);// 如果sk对应的Channel包含客户端的连接请求if (sk.isAcceptable()) {// 调用accept方法接收连接,产生服务器段的SocketChennalSocketChannel sc = server.accept();// 设置采用非阻塞模式sc.configureBlocking(false);// 将该SocketChannel注册到selectorsc.register(selector, SelectionKey.OP_READ);}// 如果sk对应的Channel有数据需要读取if (sk.isReadable()) {// 获取该SelectionKey对银行的Channel,该Channel中有刻度的数据SocketChannel sc = (SocketChannel) sk.channel();// 定义备注执行读取数据源的ByteBufferByteBuffer buff = ByteBuffer.allocate(1024);String content = "";// 开始读取数据try {while (sc.read(buff) > 0) {buff.flip();content += charse.decode(buff);}System.out.println("读取的数据:" + content);// 将sk对应的Channel设置成准备下一次读取sk.interestOps(SelectionKey.OP_READ);}// 如果捕获到该sk对银行的Channel出现了异常,表明// Channel对应的Client出现了问题,所以从Selector中取消catch (IOException io) {// 从Selector中删除指定的SelectionKeysk.cancel();if (sk.channel() != null) {sk.channel().close();}}// 如果content的长度大于0,则连天信息不为空if (content.length() > 0) {// 遍历selector里注册的所有SelectionKeyfor (SelectionKey key : selector.keys()) {// 获取该key对应的ChannelChannel targerChannel = key.channel();// 如果该Channel是SocketChannel对象if (targerChannel instanceof SocketChannel) {// 将读取到的内容写入该Channel中SocketChannel dest = (SocketChannel) targerChannel;dest.write(charse.encode(content));}}}}}}}public static void main(String [] args) throws IOException{new NServer().init();}}

客户端

package com.java.xiong.Net17;import java.io.IOException;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.nio.charset.Charset;import java.util.Scanner;public class NClient {//定义检测Sockethannel的Selector对象private Selector  selector=null;static final int PORT=30000;//定义处理编码的字符集private Charset charset=Charset.forName("GBK");//客户端SocketChannelprivate SocketChannel sc=null;public void init() throws IOException{selector=Selector.open();InetSocketAddress isa=new InetSocketAddress("127.0.0.1", PORT);//调用open的静态方法创建连接指定的主机的SocketChannelsc=SocketChannel.open(isa);//设置该sc已非阻塞的方式工作sc.configureBlocking(false);//将SocketChannel对象注册到指定的Selectorsc.register(selector, SelectionKey.OP_READ);//启动读取服务器数据端的线程new ClientThread().start();//创建键盘输入流Scanner scan=new Scanner(System.in);while(scan.hasNextLine()){//读取键盘的输入String line=scan.nextLine();//将键盘的内容输出到SocketChanenel中sc.write(charset.encode(line));}}//定义读取服务器端的数据的线程private class ClientThread extends Thread{@Overridepublic void run() {try{while(selector.select()>0){//遍历每个有可能的IO操作的Channel对银行的SelectionKeyfor(SelectionKey sk:selector.selectedKeys()){//删除正在处理的SelectionKeyselector.selectedKeys().remove(sk);//如果该SelectionKey对应的Channel中有可读的数据if(sk.isReadable()){//使用NIO读取Channel中的数据SocketChannel sc=(SocketChannel)sk.channel();String content="";ByteBuffer bff=ByteBuffer.allocate(1024);while(sc.read(bff)>0){sc.read(bff);bff.flip();content+=charset.decode(bff);}//打印读取的内容System.out.println("聊天信息:"+content);sk.interestOps(SelectionKey.OP_READ);}}}}catch(IOException io){io.printStackTrace();}}}public static void main(String [] args) throws IOException{new NClient().init();}}


原创粉丝点击