zoukankan      html  css  js  c++  java
  • 利用NIO的Selector处理服务器-客户端模型

    package NIOTEST;
    
    
    import java.io.IOException;
    import java.net.InetAddress;
    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;
    import java.util.Set;
    
    public class NIOServer {
        public static void main(String[] args) throws IOException, InterruptedException {
            Selector selector = Selector.open();
    
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), 8080);
            serverSocketChannel.socket().bind(address);
            serverSocketChannel.configureBlocking(false);
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
    
            while (true) {
                if (selector.select() > 0) {
                    Set<SelectionKey> selectionKeys = selector.selectedKeys();
                    Iterator<SelectionKey> it = selectionKeys.iterator();
                    while (it.hasNext()) {
                        SelectionKey selectionKey = it.next();
                        if (selectionKey.isAcceptable()) {
                            serverSocketChannel = (ServerSocketChannel)selectionKey.channel();
                            SocketChannel socketChannel = serverSocketChannel.accept();
                            socketChannel.configureBlocking(false);
                            socketChannel.register(selector, SelectionKey.OP_READ);
                            System.out.println("Connected: " + socketChannel.socket().getRemoteSocketAddress());
                        } else if (selectionKey.isReadable()) {
                            SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                            ByteBuffer buffer = ByteBuffer.allocate(1024);
                            while (socketChannel.read(buffer) > 0) {
                                buffer.flip();
                                byte[] dis = new byte[buffer.limit()];
                                buffer.get(dis);
                                System.out.println("当前线程="+Thread.currentThread().getId()+"--"+new String(dis));
                            }
                        }
    
                        it.remove();
                    }
                }
    
                Thread.sleep(100);
            }
        }
    }

    客户端

    package NIOTEST;
    
    import java.io.IOException;
    import java.net.InetAddress;
    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.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.Set;
    public class NIOClient {
        public static void main(String[] args) throws IOException {
            SocketChannel socketChannel = SocketChannel.open();
            InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), 8080);
            socketChannel.socket().connect(address);
    
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while (true) {
                try {
                    buffer.clear();
                    String time = sdf.format(new Date());
                    buffer.put(time.getBytes());
                    buffer.flip();
                    socketChannel.write(buffer);
                    Thread.sleep(5000);
                } catch (Exception e) {
                    System.out.println("Connection Close");
                    break;
                }
            }
        }
    }
    View Code
  • 相关阅读:
    字符串属于对象而非基本数据类型,为什么?
    比较字符串内容用equals,比较字符串的地址用==,为什么?
    服务器数据恢复方法,磁盘阵列崩溃怎么恢复数据
    服务器磁盘阵列数据恢复,raid5两块硬盘掉线数据恢复方法
    服务器存储共享文件夹丢失数据恢复检测报告
    SUN平台服务器光纤共享存储互斥失败如何恢复数据?
    服务器磁盘阵列存储瘫痪数据恢复成功案例
    EMC CX4-480服务器raid磁盘数据恢复案例
    服务器数据恢复方法_存储raid硬盘离线数据恢复案例
    山西某公司NetApp存储不小心删除文件数据恢复成功案例
  • 原文地址:https://www.cnblogs.com/tiancai/p/8944705.html
Copyright © 2011-2022 走看看