• 让Netty入门变得简单


    让Netty入门变得简单

    https://mp.weixin.qq.com/s/MBnbLmCmFJo0QK9WNwXrXQ

    如果先启动nettyClient就不会有nettyServer输出了;

    package com.stono;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class IOServer {
    
        public static void main(String[] args) throws IOException {
            ServerSocket serverSocket = new ServerSocket(8000);
            new Thread(() -> {
                while (true) {
                    try{
                        Socket socket = serverSocket.accept();
                        new Thread(() -> {
                            try{
                                byte[] data = new byte[1024];
                                InputStream inputStream = socket.getInputStream();
                                while (true) {
                                    int len;
                                    while ((len = inputStream.read(data)) != -1) {
                                        System.out.println(new String(data, 0, len));
                                    }
                                }
                            }catch (Exception e){
    
                            }
                        }).start();
                    }catch (Exception e){
                    }
                }
            }).start();
        }
    }
    package com.stono;
    
    import java.net.Socket;
    import java.util.Date;
    
    public class IOClient {
        public static void main(String[] args) {
            new Thread(() -> {
                try {
                    Socket socket = new Socket("127.0.0.1", 8000);
                    while (true) {
                        try {
                            socket.getOutputStream().write((new Date() + ": hello world").getBytes());
                            socket.getOutputStream().flush();
                            Thread.sleep(2000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
    
        }
    }
    package com.stono;
    
    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.channel.ChannelHandlerContext;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.SimpleChannelInboundHandler;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.channel.socket.nio.NioSocketChannel;
    import io.netty.handler.codec.string.StringDecoder;
    
    public class NettyServer {
    
        public static void main(String[] args) {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            NioEventLoopGroup boos = new NioEventLoopGroup();
            NioEventLoopGroup worker = new NioEventLoopGroup();
            serverBootstrap
                    .group(boos, worker)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
    
                        @Override
                        protected void initChannel(NioSocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new StringDecoder());
                            ch.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
                                @Override
                                protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
                                    System.out.println(s);
                                }
                            });
    
                        }
                    })
                    .bind(8000);
        }
    }
    package com.stono;
    
    import io.netty.bootstrap.Bootstrap;
    import io.netty.channel.Channel;
    import io.netty.channel.ChannelInitializer;
    import io.netty.channel.EventLoopGroup;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.nio.NioSocketChannel;
    import io.netty.handler.codec.string.StringEncoder;
    
    import java.util.Date;
    import java.util.concurrent.TimeUnit;
    
    public class NettyClient {
        public static void main(String[] args) throws InterruptedException {
            Bootstrap bootstrap = new Bootstrap();
            NioEventLoopGroup group = new NioEventLoopGroup();
            bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<Channel>() {
                        @Override
                        protected void initChannel(Channel channel) throws Exception {
                            channel.pipeline().addLast(new StringEncoder());
                        }
                    });
    
            Channel channel = bootstrap.connect("127.0.0.1", 8000).channel();
            while (true) {
                channel.writeAndFlush(new Date() + ": hello world");
                TimeUnit.SECONDS.sleep(2);
            }
    
    
        }
    }
  • 相关阅读:
    Appium之开发计算器自动化测试脚本Demo
    Appium之开发环境搭建
    Javassist之常用API的应用 02
    阿里云提出的漏洞(Phpcms V9某处逻辑问题导致getshell漏洞解决方法)的问题
    z-index 层级关系
    html5移动端Meta设置
    js判断手机访问PC端跳转到手机站
    PHPCMS如何开启手机站点
    DedeCms文档关键词替换,优先替换长尾关键词
    [转载]利用@media screen实现网页布局的自适应,@media screen and
  • 原文地址:https://www.cnblogs.com/stono/p/9198407.html
Copyright © 2020-2023  润新知