Socket-Worker模式

介绍

Socket-客户端和服务端通信改造

服务端

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ServerSocket {
    public static void main(String[] args) throws IOException {
        ServerSocketChannel server = ServerSocketChannel.open();
        server.configureBlocking(Boolean.FALSE);
        server.bind(new InetSocketAddress(8888));

        Selector selector = Selector.open();
        SelectionKey selectionKey = server.register(selector, SelectionKey.OP_ACCEPT, null);
        Worker worker = new Worker("worker-0");
        while(true){
            selector.select();
            Iterator<SelectionKey> selectionKeyIterator = selector.selectedKeys().iterator();
            while (selectionKeyIterator.hasNext()){
                SelectionKey key = selectionKeyIterator.next();
                selectionKeyIterator.remove();
                if(key.isAcceptable()){
                    ServerSocketChannel serverSocketChannel  = (ServerSocketChannel) key.channel();
                    SocketChannel client = serverSocketChannel.accept();
                    client.configureBlocking(Boolean.FALSE);
                    System.out.println("注册前");
                    worker.register(client);
                    System.out.println("注册后");
                }
            }

        }
    }
    static class Worker implements Runnable{
        private Selector selector;
        private String name;
        private Thread thread;
        private Boolean initd = Boolean.FALSE;
        private ConcurrentLinkedQueue<Runnable> runnables = new ConcurrentLinkedQueue<>();
        public Worker(String name) {
            this.name = name;
        }

        public void register(SocketChannel socketChannel) throws IOException {
            runnables.add(() -> {
                SelectionKey selectionKey = null;
                try {
                    selectionKey = socketChannel.register( this.selector, 0, ByteBuffer.allocate(16));
                    selectionKey.interestOps(SelectionKey.OP_READ);
                } catch (ClosedChannelException e) {
                    e.printStackTrace();
                }
            });

            if(!initd){
                this.selector = Selector.open();
                this.thread = new Thread(this);
                this.thread.setName(this.name);
                this.thread.start();
                this.initd = Boolean.TRUE;
            }



        }

        @Override
        public void run() {
            while (true){
                try {
                    Iterator<Runnable> iterator = runnables.iterator();
                    while(iterator.hasNext()){
                        System.out.println("注册");
                        Runnable runnable = iterator.next();
                        iterator.remove();
                        runnable.run();
                    }
                    this.selector.select();
                    Iterator<SelectionKey> selectionKeyIterator = this.selector.selectedKeys().iterator();
                    while (selectionKeyIterator.hasNext()){
                        SelectionKey selectionKey = selectionKeyIterator.next();
                        selectionKeyIterator.remove();
                        if(selectionKey.isReadable()){
                            SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
                            ByteBuffer buffer = (ByteBuffer) selectionKey.attachment();
                            socketChannel.read(buffer);
                            buffer.flip();
                            if(buffer.limit() == buffer.capacity()){
                                ByteBuffer bufferNew = ByteBuffer.allocate( buffer.capacity() * 2);
                                bufferNew.put(buffer);
                                selectionKey.attach(bufferNew);
                            }else{
                                System.out.println("work-读数据");
                                System.out.println(StandardCharsets.UTF_8.decode(buffer));
                                buffer.clear();
                                selectionKey.attach(ByteBuffer.allocate(16));
                            }

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

客户端

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class ClientSocket {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("localhost", 8888));
        System.out.println(socketChannel.isConnected());
        Scanner scanner = new Scanner(System.in);
        while(true){
            String content = scanner.next();
            if("read".equals(content)){
                socketChannel.write(StandardCharsets.UTF_8.encode(content));
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                socketChannel.read(buffer);
                buffer.flip();
                System.out.println(StandardCharsets.UTF_8.decode(buffer));
            }
        }
    }
}

相关推荐

  1. Socket-Worker模式

    2024-01-16 20:52:09       38 阅读
  2. Worker-Thread设计模式

    2024-01-16 20:52:09       44 阅读
  3. node.js中 cluster 模块worker_threads 模块

    2024-01-16 20:52:09       9 阅读
  4. 设计模式之多线程分工模式---Worker Thread模式

    2024-01-16 20:52:09       42 阅读
  5. web worker

    2024-01-16 20:52:09       31 阅读
  6. C语言中socket模块、线程

    2024-01-16 20:52:09       27 阅读
  7. Web Workers 介绍

    2024-01-16 20:52:09       13 阅读

最近更新

  1. springBoot整合mongodb

    2024-01-16 20:52:09       0 阅读
  2. STM32 系统时钟初始化函数和延时函数

    2024-01-16 20:52:09       0 阅读
  3. Oracle数据库服务器CPU占用率巨高的问题排查思路

    2024-01-16 20:52:09       0 阅读
  4. WebKit简介及工作流程

    2024-01-16 20:52:09       0 阅读
  5. nlp中tokenizer用法

    2024-01-16 20:52:09       1 阅读

热门阅读

  1. Snakemake:初探

    2024-01-16 20:52:09       36 阅读
  2. 融优学堂-艺术史

    2024-01-16 20:52:09       24 阅读
  3. computed和watch和watchEffect 相同和不同

    2024-01-16 20:52:09       37 阅读
  4. 【速成】蓝桥杯嵌入式省一教程

    2024-01-16 20:52:09       40 阅读
  5. sql语句

    2024-01-16 20:52:09       30 阅读
  6. ROS安装:一行代码安装完成ROS

    2024-01-16 20:52:09       33 阅读
  7. hnswlib报错ItemCannotBeInsertedIntoTheVectorSpaceException

    2024-01-16 20:52:09       33 阅读
  8. SQLite,ROOM 清空表数据并将自增量归零

    2024-01-16 20:52:09       34 阅读
  9. openssl3.2 - 官方demo学习 - cms - cms_sign2.c

    2024-01-16 20:52:09       28 阅读
  10. CSDN - Python中新手入门----------元组

    2024-01-16 20:52:09       38 阅读
  11. 算法 - 回溯 / DFS / BFS

    2024-01-16 20:52:09       31 阅读