NIO(3)デモは、サービスとクライアントを達成するために

免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
このリンク: 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、生成のソケットSockertChannel()メソッドServerSocketを
加工し2.SelectionKeyの採取後は需要清空ここで達成する明確な方法があります。また、横断中の治療、明確にすること

2つのクライアント

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の直接接続方法によって達成されます
//;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. クライアントは、(サーバーがメッセージを送信)入力ストリームを待っている必要があり、また、我々は、ユーザの入力を待つ必要がサーバーに送信されます。だから、二つのスレッド、スレッドは、入力された成功は、チャネルを書きますと、コンソール入力を書くために待っています。

参照

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

おすすめ

転載: blog.csdn.net/m0_38060977/article/details/102754805