netty之NettyServer接收数据

前言

对于一个初学者,不建议上来就研究理论,实操往往更重要。本章节介绍使用netty端写一个能接收数据的socketServer服务端,通过实现通道适配器ChannelInboundHandlerAdapter.channelRead获取并并解析接收数据

一:MyChannelInitializer

java 复制代码
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel channel) {

        System.out.println("链接报告开始");
        System.out.println("链接报告信息:有一客户端链接到本服务端");
        System.out.println("链接报告IP:" + channel.localAddress().getHostString());
        System.out.println("链接报告Port:" + channel.localAddress().getPort());
        System.out.println("链接报告完毕");

        //在管道中添加我们自己的接收数据实现方法
        channel.pipeline().addLast(new MyServerHandler());

    }

MyServerHandler

java 复制代码
public class MyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //接收msg消息
        ByteBuf buf = (ByteBuf) msg;
        byte[] msgByte = new byte[buf.readableBytes()];
        buf.readBytes(msgByte);
        System.out.print(new Date() + "接收到消息:");
        System.out.println(new String(msgByte, Charset.forName("GBK")));
    }
}
java 复制代码
public class NettyServer {
    public static void main(String[] args) {
        new NettyServer().bing(7397);
    }

    private void bing(int port) {
        //配置服务端NIO线程组
        //NioEventLoopGroup extends MultithreadEventLoopGroup Math.max(1, SystemPropertyUtil.getInt("io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
        EventLoopGroup parentGroup = new NioEventLoopGroup();
        EventLoopGroup childGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(parentGroup, childGroup)
                    .channel(NioServerSocketChannel.class)    //非阻塞模式
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childHandler(new MyChannelInitializer());
            ChannelFuture f = b.bind(port).sync();
            System.out.println("com.lm.netty03 server start done. {关注码农明哥:获取源码}");
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            childGroup.shutdownGracefully();
            parentGroup.shutdownGracefully();
        }

    }
}

启动

NettyServer

好了到这里就结束了基础netty之NettyServer接收数据的学习,大家一定要跟着动手操作起来。需要的源码的 可si我获取;

相关推荐
Run_Teenage36 分钟前
C++:智能指针的使用及其原理
开发语言·c++·算法
WX-bisheyuange2 小时前
基于Spring Boot的民宿预定系统的设计与实现
java·spring boot·后端·毕业设计
码界奇点2 小时前
Java设计模式精讲从基础到实战的常见模式解析
java·开发语言·设计模式·java-ee·软件工程
四维碎片2 小时前
【Qt】配置安卓开发环境
android·开发语言·qt
百***99242 小时前
MySql的慢查询(慢日志)
android·mysql·adb
西游音月2 小时前
(7)框架搭建:Qt实战项目之主窗体导航栏、状态栏
开发语言·qt
安卓兼职framework应用工程师2 小时前
android 15.0 Launcher3长按拖拽时,获取当前是哪一屏,获取当前多少个应用图标
android·拖拽·workspace·长按拖拽
3***49962 小时前
Swift Experience
开发语言·ios·swift
iFlow_AI2 小时前
iFlow CLI Hooks 「从入门到实战」应用指南
开发语言·前端·javascript·人工智能·ai·iflow·iflow cli
Maybyy3 小时前
Chart.js图标绘制工具库
开发语言·javascript·ecmascript