服务端启动流程

package com.example.netty;

import com.example.netty.handler.HelloServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; public class HelloServer { public static void main(String[] args) {
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
NioEventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap serverBootstrap = new ServerBootstrap();
//1. 指定线程组
serverBootstrap.group(bossGroup, workerGroup)
.localAddress(8000)//2. 指定端口
.channel(NioServerSocketChannel.class)//3. 指定IO模型
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new HelloServerHandler());
}
});//4. 配置业务处理逻辑类
//5. 绑定端口
serverBootstrap.bind().addListener((future)->{
if(future.isSuccess()){
System.out.println("端口绑定成功");
}else{
System.out.println("端口绑定失败:"+future.cause());
}
});
} }
  1. bossGroupworkerGroup可以看作是传统IO网络编程的两个线程组,bossGroup负责 accept 新的socket连接,workerGroup负责socket连接的读写。
  2. ServerBootstrap是服务端引导类,负责.group(bossGroup, workerGroup)配置线程模型;.channel指定IO模型,NioServerSocketChannel.class是NIO模型,OioServerSocketChannel.class是传统IO模型;.childHandler配置业务逻辑处理。
  3. .bind()绑定端口,该方法是异步执行,所以需要配置监听器。

服务端业务处理类

package com.example.netty.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil; public class HelloServerHandler extends ChannelInboundHandlerAdapter { @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf byteBuf = (ByteBuf) msg;
System.out.println(byteBuf.toString(CharsetUtil.UTF_8));
ctx.writeAndFlush(Unpooled.copiedBuffer("hello client".getBytes()));
}
}

主要打印客户端发送的消息并返回Hello Client。

客户端启动流程

package com.example.netty;

import com.example.netty.handler.HelloClientHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; public class HelloClient { public static void main(String[] args) {
NioEventLoopGroup workerGroup = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap();
//1. 配置线程组
bootstrap.group(workerGroup)
.channel(NioSocketChannel.class)//2. 指定IO模型
.remoteAddress("127.0.0.1", 8000)//3. 指定连接ip和端口
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new HelloClientHandler());
}
});//4. 配置业务处理逻辑
//5. 连接
bootstrap.connect().addListener(future -> {
if(future.isSuccess()){
System.out.println("连接成功");
}else{
System.out.println("连接失败:" + future.cause());
}
});
} }

客户端引导类为Bootstrap,而服务端为ServerBootstrap

业务处理逻辑类

package com.example.netty.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil; public class HelloClientHandler extends ChannelInboundHandlerAdapter { @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer("hello server".getBytes()));
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf byteBuf = (ByteBuf) msg;
System.out.println("recieve from server:" + byteBuf.toString(CharsetUtil.UTF_8));
}
}

主要在连接后向服务端发送Hello Server,并接受打印服务端返回消息。

最新文章

  1. RNN求解过程推导与实现
  2. mariadb用户和权限管理
  3. Unity multi_compile
  4. echo颜色显示
  5. C# @Page指令中的AutoEventWireup,CodeBehind,Inherits
  6. nginx负载均衡和反向代理有什么区别
  7. JSON--List集合转换成JSON对象
  8. jQuery中要注意的一些函数
  9. WINDOWS Server2003上部署一个Asp.Net的网站
  10. centos 安装 vsftp
  11. Oracle数据库之序列
  12. ASP.NET MVC 單元測試系列
  13. JavaScript异步编程
  14. 简单易上手的Bootstrap
  15. 关于在vim中的查找和替换
  16. c++11のunique_lock和once_flag
  17. jQuery实现画面的展开、收起和停止
  18. 【LOJ#6073】距离(主席树)
  19. 7 个最佳的 Java 框架
  20. JavaScript数字转字符串,字符串转数字

热门文章

  1. python0.1
  2. socket 建立网络连接,client &amp;&amp; server
  3. 让对象拥有状态——C#中的状态模式
  4. C++中string转换为char*类型返回后乱码问题
  5. let import export React入门实例教程 connect provider combineReducers 箭头函数 30分钟掌握ES6/ES2015核心内容 Rest babel
  6. SCSS笔记
  7. 分享一个集成.NET Core+Swagger+Consul+Polly+Ocelot+IdentityServer4+Exceptionless+Apollo+SkyWalking的微服务开发框架
  8. Python-利用xlrd模块操作excel
  9. day08总结
  10. day32 异常处理、网络编程