zoukankan      html  css  js  c++  java
  • 基于java NIO 的服务端与客户端代码

    在对java NIO  selector 与 Buffer Channel  有一定的了解之后,我们进行编写java nio 实现的 客户端与服务端例子:

    服务端:

    public class NIOChatService {
        
        private static Map<String,SocketChannel> userMap =new HashMap<>();
        public static void main(String[] args) throws Exception {
            
            //创建selector 对象
            Selector selector =Selector.open();
            //创建serverSocketChannel 
            ServerSocketChannel serverSocketChannel =ServerSocketChannel.open();
            //设置为非阻塞
            serverSocketChannel.configureBlocking(false);
            //绑定端口
            serverSocketChannel.bind(new InetSocketAddress(9999));
            //监听客户端的连接事件,绑定在selector 上
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
            //死循环
            while(true) {
                
                //阻塞监听,监听是否有事件发生
                selector.select();
                //获取事件发生的selectionkey
                Set<SelectionKey>     selectedKeys = selector.selectedKeys();
                
                for(SelectionKey key:selectedKeys) {
                    if(key.isAcceptable()) {
                    //如果是客户端的连接的话
                    ServerSocketChannel serverchannel=(ServerSocketChannel)key.channel();
                    //获取socketchannel 传输对象
                    SocketChannel accept = serverchannel.accept();
                    accept.configureBlocking(false);
                    accept.register(selector, SelectionKey.OP_READ);
                    //保存连接的用户信息
                    String keys ="{"+UUID.randomUUID()+"}";
                    userMap.put(keys, accept);
                    System.out.println(userMap);
                }
                    
                    else if(key.isReadable()) {
                        //只要能进入到可读的,那么channel 一定是SocketChannel
                        SocketChannel socketChannel =(SocketChannel)key.channel();
                        
                        ByteBuffer bytebuffer =ByteBuffer.allocate(1024);
                        
                        //读取数据到ByteBuffer
                        
                        socketChannel.read(bytebuffer);
                        
                        bytebuffer.flip();
                        Charset charset =Charset.forName("utf-8");
                        String receiveMsg=String.valueOf(charset.decode(bytebuffer).array());
                        
                        
                        
                        for(Map.Entry<String, SocketChannel> entry:userMap.entrySet()) {
                            
                            ByteBuffer bys1 =ByteBuffer.allocate(1024);
                            bys1.put((entry.getKey()+receiveMsg).getBytes());
                            SocketChannel channel =entry.getValue();
                            bys1.flip();
                            channel.write(bys1);
                            
                        }
                        
                        
                        
                    }
                    //一定要进行clear 操作
                    selectedKeys.clear();
                }
                
                
                
                ;
                
                
            }
            
        }
    
    }

    客户端例子:

    public class JavaNioClient {
        
        
        public static void main(String[] args) throws Exception {
            
            
            //建立Selector
            Selector selector =Selector.open();
            SocketChannel socketChannel=SocketChannel.open();
            //设置非阻塞
            socketChannel.configureBlocking(false);
            
            socketChannel.register(selector, SelectionKey.OP_CONNECT);
            
            socketChannel.connect(new InetSocketAddress("127.0.0.1", 9999));
            
            
            while(true) {
                selector.select();
                
                Set<SelectionKey> setionkey =selector.selectedKeys();
                
                for(SelectionKey kk :setionkey) {
                    if(kk.isConnectable()) {
                        //从selectionkey 获取socketChannel
                        SocketChannel socket=(SocketChannel)kk.channel();
                        //手动建立连接
                        if(socket.isConnectionPending()) {
                            socket.finishConnect();
                            //写数据
                            ByteBuffer byteB = ByteBuffer.allocate(1024);
                            byteB.put((System.currentTimeMillis()+"连接ok").getBytes());
                            byteB.flip();
                            socket.write(byteB);
                            //jdk1.5 线程池
                            ExecutorService exe =Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
                            
                            exe.submit(new Thread() {
                                
                                @Override
                                public void run() {
                                    while(true) {
                                        String msg=null;
                                    byteB.clear();
                                    //标准键盘输入
                                    BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
                                    try {
                                         msg =br.readLine();
                                         ByteBuffer bytec = ByteBuffer.allocate(1024);
                                         bytec.put(msg.getBytes());
                                         bytec.flip();
                                         socket.write(bytec);
                                    } catch (IOException e) {
                                        
                                        e.printStackTrace();
                                    }
                                    }
                                }
                            });
                        }
                        
                        
                        socket.register(selector, SelectionKey.OP_READ);
                    }
                    else if(kk.isReadable())  {
                        
                        //从selectionkey 获取socketChannel
                        SocketChannel socket=(SocketChannel)kk.channel();
                        ByteBuffer by =ByteBuffer.allocate(1024);
                        int a=socket.read(by);
                        //if(a>0) {
                        String receive =new String(by.array());
                        System.out.println(receive);
                        //}
                    }
                    
                    setionkey.clear();
                }
                
            }
            
            
        }
    
    }
    原创打造,多多指教
  • 相关阅读:
    Atitit.eclise的ide特性-------abt 编译
    Atitit.eclise的ide特性-------abt 编译
    Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Python3.6新特性2  Python2.7新特性Python 2.7的新特性
    Atitit python3.0 3.3 3.5 3.6 新特性 Python2.7新特性1Python 3_x 新特性1python3.4新特性1python3.5新特性1值得关注的新特性1Pyth
    Atitit intellij idea的使用总结attilax
    Atitit.eclipse 4.3 4.4  4.5 4.6新特性
    Atitit.eclipse 4.3 4.4  4.5 4.6新特性
    Atitit.eclipse 4.3 4.4  4.5 4.6新特性
    Atitit 软件国际化原理与概论
    Atitit 软件国际化原理与概论
  • 原文地址:https://www.cnblogs.com/iscys/p/9690026.html
Copyright © 2011-2022 走看看