nio(3)demo实现服务端和客户端

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/m0_38060977/article/details/102754805

一 服务端

package com.zyc.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
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.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @author zhuyc
 * @create 2019-03-27 20:49
 */
public class NioServer {

    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(new InetSocketAddress(8899));

        Selector selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        Map<String,SocketChannel> clientMap = new HashMap<>();

        while (true){
            try {
                selector.select();
                Set<SelectionKey> selectionKeys = selector.selectedKeys();

                selectionKeys.forEach(selectionKey -> {
                    SocketChannel client;
                    try {
                        if(selectionKey.isAcceptable()){
                            ServerSocketChannel server = (ServerSocketChannel) selectionKey.channel();
                            client = server.accept();
                            client.configureBlocking(false);
                            client.register(selector,SelectionKey.OP_READ);

                            String key = "["+ UUID.randomUUID().toString() +"]";
                            clientMap.put(key,client);

                        }else if (selectionKey.isReadable()){
                            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                            client = (SocketChannel) selectionKey.channel();

                            int count = client.read(byteBuffer);

                            if(count > 0){
                                byteBuffer.flip();

                                Charset charset = Charset.forName("UTF-8");

                                String receiveMsg = String.valueOf(charset.decode(byteBuffer).array());

                                System.out.println(client +" : "+receiveMsg);


                                String clinetKey = null;
                                for(Map.Entry<String,SocketChannel> entry:clientMap.entrySet()){
                                    if(client == entry.getValue()){
                                        clinetKey = entry.getKey();
                                        break;

                                    }


                                }
                                for(Map.Entry<String,SocketChannel> entry:clientMap.entrySet()){
                                    SocketChannel sc = entry.getValue();
                                    ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
                                    writeBuffer.put((clinetKey +": "+receiveMsg).getBytes());
                                    writeBuffer.flip();
                                    sc.write(writeBuffer);

                                }
                            }
//                            while(count > 0){
//                                System.out.println(count);
//                                byteBuffer.clear();
//                                count = client.read(byteBuffer);
//
//
//
//
//                            }



                            System.out.println("end reading");
                        }



                    }catch (Exception ex){
                        ex.printStackTrace();
                    }


                });
                selectionKeys.clear();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

}

注意点

1.服务端绑定地址仍是通过ServerSocket,ServerSocket由SockertChannel的socket()方法产生
2.SelectionKey的集合处理完之后需要清空。这里是用clear方法实现的。也可以在遍历过程中处理一个,清空一个

二 客户端

package com.zyc.nio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.time.LocalDateTime;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author zhuyc
 * @create 2019-03-31 9:10
 */
public class NioClient {

    public static void main(String[] args) {
        SocketChannel socketChannel = null;
        try {
            socketChannel = SocketChannel.open();
            socketChannel.configureBlocking(false);
            Socket socket = socketChannel.socket();

            Selector selector = Selector.open();
            socketChannel.register(selector, SelectionKey.OP_CONNECT);
            socketChannel.connect(new InetSocketAddress("127.0.0.1",8899));

            while (true){
                selector.select();
                Set<SelectionKey> selectionKeys = selector.selectedKeys();

                for (SelectionKey selectionKey:selectionKeys){
                    if(selectionKey.isConnectable()){
                        SocketChannel client = (SocketChannel)selectionKey.channel();
                        if(client.isConnectionPending()){
                            client.finishConnect();

                            ByteBuffer writeBuffer = ByteBuffer.allocate(1024);
                            writeBuffer.put((LocalDateTime.now()+" connect success").getBytes());
                            writeBuffer.flip();
                            client.write(writeBuffer);

                            ExecutorService executorService = Executors.newSingleThreadExecutor();
                            executorService.submit(new Runnable() {
                                @Override
                                public void run() {
                                    InputStreamReader inputStreamReader = new InputStreamReader(System.in);
                                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                                    try {
                                        String content = bufferedReader.readLine();

                                        writeBuffer.clear();
                                        writeBuffer.put(content.getBytes());
                                        writeBuffer.flip();
                                        client.write(writeBuffer);

                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }


                                }
                            });
                            client.register(selector,SelectionKey.OP_READ);


                        }


                    }else  if(selectionKey.isReadable()){
                        SocketChannel sc = (SocketChannel) selectionKey.channel();
                        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

                        int count  = sc.read(byteBuffer);

                        if(count > 0){
                            String receiveMsg = new String(byteBuffer.array(),0,count);
                            System.out.println(receiveMsg);
                        }
                    }

                }
                selectionKeys.clear();

            }


        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}

注意点

  1. 客户端访问地址是直接通过SocketChannel的connect方法实现的
//;SocketChannel支持在非阻塞模式下连接:connect方法发起远程TCP连接,但是其立即返回,
//不过连接的过程仍然在进行,之后需要通过finishConnect方法检测并完成链接,可以通过isConnectionPending()检测链接是否正在进行.
 if(client.isConnectionPending()){
  client.finishConnect();
  ...

源码注释说明

    /**
     * Tells whether or not a connection operation is in progress on this
     * channel.
     *
     * @return  <tt>true</tt> if, and only if, a connection operation has been
     *          initiated on this channel but not yet completed by invoking the
     *          {@link #finishConnect finishConnect} method
     */
    public abstract boolean isConnectionPending();
  1. 客户端需要一直等待输入流(服务端发送消息),也需要等待用户输入发送给服务端。所以需要两个线程,写线程一直等待控制台输入,一旦输入成功便写入channel。

参考

1.https://shift-alt-ctrl.iteye.com/blog/1840409

猜你喜欢

转载自blog.csdn.net/m0_38060977/article/details/102754805
今日推荐