设为首页 加入收藏

TOP

也谈Reactor模式(二)
2019-09-02 23:09:12 】 浏览:55
Tags:也谈 Reactor 模式
);
40 }else if (key.isReadable()){ 41 doRead(key); 42 }else if (key.isWritable() && key.isValid()){ 43 doWrite(key); 44 }else if (key.isConnectable()){ 45 System.out.println("连接成功!"); 46 } 47 } 48 } 49 } 50 51 public void doAccept(SelectionKey key) throws IOException { 52 ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel(); 53 System.out.println("ServerSocketChannel正在循环监听"); 54 SocketChannel clientChannel = serverChannel.accept(); 55 clientChannel.configureBlocking(false); 56 clientChannel.register(key.selector(),SelectionKey.OP_READ); 57 } 58 59 public void doRead(SelectionKey key) throws IOException { 60 SocketChannel clientChannel = (SocketChannel) key.channel(); 61 ByteBuffer byteBuffer = ByteBuffer.allocate(BUF_SIZE); 62 long bytesRead = clientChannel.read(byteBuffer); 63 while (bytesRead>0){ 64 byteBuffer.flip(); 65 byte[] data = byteBuffer.array(); 66 String info = new String(data).trim(); 67 System.out.println("从客户端发送过来的消息是:"+info); 68 byteBuffer.clear(); 69 bytesRead = clientChannel.read(byteBuffer); 70 } 71 if (bytesRead==-1){ 72 clientChannel.close(); 73 } 74 } 75 76 public void doWrite(SelectionKey key) throws IOException { 77 ByteBuffer byteBuffer = ByteBuffer.allocate(BUF_SIZE); 78 byteBuffer.flip(); 79 SocketChannel clientChannel = (SocketChannel) key.channel(); 80 while (byteBuffer.hasRemaining()){ 81 clientChannel.write(byteBuffer); 82 } 83 byteBuffer.compact(); 84 } 85 86 public static void main(String[] args) throws IOException { 87 MyNioServer myNioServer = new MyNioServer(); 88 myNioServer.initServer(); 89 } 90 } View Code

客户端:

 1 package cn.blog.test.NioTest;
 2 
 3 
 4 import java.io.IOException;
 5 import java.net.InetSocketAddress;
 6 import java.nio.ByteBuffer;
 7 import java.nio.channels.SelectionKey;
 8 import java.nio.channels.Selector;
 9 import java.nio.channels.SocketChannel;
10 import java.util.Iterator;
11 
12 public class MyNioClient {
13     private Selector selector;          //创建一个选择器
14     private final static int port = 8686;
15     private final static int BUF_SIZE = 10240;
16     private static ByteBuffer byteBuffer = ByteBuffer.allocate(BUF_SIZE);
17 
18     private void  initClient() throws IOException {
19         this.selector = Selector.open();
20         SocketChannel clientChannel = SocketChannel.open();
21         clientChannel.configureBlocking(false);
22         clientChannel.connect(new InetSocketAddress(port));
23         clientChannel.register(selector, SelectionKey.OP_CONNECT);
24         while (true){
25             selector.select();
26             Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
27             while (iterator.hasNext()){
28                 SelectionKey key = iterator.next();
29                 iterator.remove();
30                 if (key.isConnectable()){
31                     doConnect(key);
32                 }else if (key.isReadable()){
33                     doRead(key);
34                 }
35             }
36         }
37     }
38 
39     public void doConnect(SelectionKey key) throws IOException {
40         SocketChannel clientChannel = (SocketChannel) key.channel();
41         if (clientChannel.isConnectionPending()){
42             clientChannel.finishConnect();
43         }
44         clientChannel.configureBlocking(false);
45         String info = "服务端你好!!";
46         byteBuffer.clear();
47         byteBuffer.put(info.getB
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇工厂方法和抽象工厂模式. 下一篇挑战常规--这样写单例是错的!

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目