netty之helloworld

简介: netty网络编程的学习

首先引入netty的依赖

       <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.39.Final</version>
        </dependency>

然后编写服务端代码

package com.wxit.netty.c1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
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;

/**
 * @author wj
 * @date 2022.11.14 22:08
 */
public class HelloServer {
    public static void main(String[] args) {
        //1.启动器,负责组装netty 组件,启动服务器
        new ServerBootstrap()
                //2. BossEventLoop WorkEventLoop(selector,thread) group 组
                .group(new NioEventLoopGroup())
                //3.选择服务器的ServerSocketChannel 实现
                .channel(NioServerSocketChannel.class)
                //4.boss 负责处理连接 work(child) 负责处理读写 ,决定了worker能执行哪些操作
                .childHandler(
                        //5.channel 代表和客户端进行数据读写的通道 Initializer 初始化,负责添加别的handler
                    new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        //6.添加具体的handler
                       ch.pipeline().addLast(new StringDecoder());
                       //自定义handler
                       ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                           @Override  //读事件
                           public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                               System.out.println(msg);
                           }
                       });
                    }
                })
                .bind(8080);
    }
}

客户端代码如下


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;

/**
 * @author wj
 * @date 2022.11.14 22:22
 */
public class HelloClient {
    public static void main(String[] args) throws InterruptedException {
        //1.启动类
        new Bootstrap()
                //2.添加EventLoop
                .group(new NioEventLoopGroup())
                //3.选择客户端 channel 实现
                .channel(NioSocketChannel.class)
                //4.添加处理器
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override  //在建立连接之后被调用
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringEncoder());
                    }
                })
                //5.连接到服务器
                .connect(new InetSocketAddress("localhost",8080))
                .sync()
                .channel()
                //6.向服务器发送数据
                .writeAndFlush("hello,world");
    }
}
相关文章
|
网络协议 前端开发 Java
Netty实战(二)第一个Netty程序
这只是一个简单的应用程序,但是**它可以伸缩到支持数千个并发连接**——每秒可以比普通的基于套接字的 Java 应用程序处理多得多的消息。
138 0
|
安全
Netty入门Demo
Netty入门Demo
106 0
Netty入门Demo
|
前端开发 Java
Netty(一)之helloworld
Netty(一)之helloworld
70 0
Netty - java.lang.NoSuchMethodError:io.netty.bootstrap.Bootstrap.channel
Netty - java.lang.NoSuchMethodError:io.netty.bootstrap.Bootstrap.channel
189 0
Netty - java.lang.NoSuchMethodError:io.netty.bootstrap.Bootstrap.channel
|
JSON 缓存 分布式计算
Netty入门 -- 什么是Netty?
Netty入门 -- 什么是Netty?
226 0
Netty入门 -- 什么是Netty?
|
前端开发
netty系列之:Bootstrap,ServerBootstrap和netty中的实现
netty系列之:Bootstrap,ServerBootstrap和netty中的实现
netty系列之:Bootstrap,ServerBootstrap和netty中的实现
|
前端开发 Java Maven
【Netty】第一个Netty应用
 前面已经学习完了Java NIO的内容,接着来学习Netty,本篇将通过一个简单的应用来了解Netty的使用。
138 0
【Netty】第一个Netty应用
|
网络协议 前端开发
Netty: WebSocket应用,代码demo
Netty: WebSocket应用,代码demo
Netty: WebSocket应用,代码demo
|
机器学习/深度学习 存储 Java
【Netty】Netty 入门案例分析 ( Netty 模型解析 | Netty 服务器端代码 | Netty 客户端代码 )
【Netty】Netty 入门案例分析 ( Netty 模型解析 | Netty 服务器端代码 | Netty 客户端代码 )
242 0
【Netty】Netty 入门案例分析 ( Netty 模型解析 | Netty 服务器端代码 | Netty 客户端代码 )