【笔记】netty客户端发数据到服务端

【笔记】netty 客户端往服务端发数据

  1. 服务端端口绑定、通道注册、事件监听
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;
import java.lang.String;

/**
 1. 服务器代码
 */
public class SelectorServer {
          
   
    public static void main(String[] args) {
          
   
        try {
          
   
            //服务端监听通道
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

            //端口绑定
            InetSocketAddress address = new InetSocketAddress(7000);
            serverSocketChannel.socket().bind(address);
            //设置非阻塞
            serverSocketChannel.configureBlocking(false);
            //selector
            Selector selector = Selector.open();
            //selector注册--监听链接事件
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
            int length = 0;
            while (true) {
          
   
                if (selector.select(5000) == 0) {
          
   
                    System.out.println("服务器等待5秒,无连接");
                    continue;
                }
                //所有有活动的事件key--与相关的channel
                Set<SelectionKey> selectionKeys = selector.selectedKeys();

                Iterator<SelectionKey> iterator = selectionKeys.iterator();

                while (iterator.hasNext()) {
          
   
                    SelectionKey selectionKey = iterator.next();

                    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                    //链接事件
                    if (selectionKey.isAcceptable()) {
          
   
                        SocketChannel socketChannel = serverSocketChannel.accept();
                        socketChannel.configureBlocking(false);
                        socketChannel.register(selector, SelectionKey.OP_READ, byteBuffer);
                    }
                    //读事件

                    if (selectionKey.isReadable()) {
          
   
                        //获取SocketChannel
                        SelectableChannel channel = selectionKey.channel();
                        SocketChannel socketChannel = (SocketChannel) channel;
                        //获取buffer--与selectionKey关联的数据
                        ByteBuffer buffer = (ByteBuffer) selectionKey.attachment();
                        socketChannel.read(buffer);
                        System.out.println("客户端发送数据:" + new String(buffer.array(), length, buffer.position() - length));
                        length = new String(buffer.array(), 0, buffer.position()).length();
                    }
                    //删除--当前事件key
                    iterator.remove();
                }
            }
        } catch (Exception e) {
          
   
            e.printStackTrace();
        }
    }
  1. 客户端连接服务端、发送数据
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class SelectorCustomer {
          
   
    public static void main(String[] args) {
          
   

        try {
          
   
            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.configureBlocking(false);

            InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 7000);

            //端口连接不成功
            if (!socketChannel.connect(inetSocketAddress)) {
          
   
                while (!socketChannel.finishConnect()) {
          
   
                    System.out.println("连接需要时间");
                }
            }
            //链接成功
            while (true) {
          
   
                //端口链接成功
                System.out.println("请输入一个字符串");
                BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
                String msg = buf.readLine();
                ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
                socketChannel.write(buffer);
            }
        } catch (Exception e) {
          
   
            e.printStackTrace();
        }
    }
经验分享 程序员 微信小程序 职场和发展