Netty

最近在学习netty练习下,先附上写的代码吧

注意不要使用5.0的版本了,官方直接废弃了,可以自己搜索下。因此只用4版本的。

<!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.50.Final</version> </dependency>

服务端的代码实现:

private static int port = 8080;

public static void main(String[] args) { _// boss__线程池负责接受请求 _ NioEventLoopGroup bossGroup = new NioEventLoopGroup(); _// work__线程池负责处理请求 _ NioEventLoopGroup workGroup = new NioEventLoopGroup(); // _创建__ServerBootstrap _ ServerBootstrap serverBootstrap = new ServerBootstrap(); _// NioServerSocketChannel__标记当前是服务器 _ serverBootstrap.group(bossGroup, workGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { // _处理每个请求__hanlder _ socketChannel.pipeline().addLast(new ServerHandler()); } }); // _绑定我们的端口号码 _ try { // _绑定端口号,同步等待成功 _ ChannelFuture future = serverBootstrap.bind(port).sync(); System.out.println("服务器启动成功:" + port); // _等待服务器监听端口 _ future.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } finally { // _优雅的关闭连接 _ bossGroup.shutdownGracefully(); workGroup.shutdownGracefully(); } }

ServerHandler类的实现

public class ServerHandler extends SimpleChannelInboundHandler { protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception { // 接受我们的数据 _ ByteBuf byteBuf = (ByteBuf) o; String request = byteBuf.toString(CharsetUtil.UTF_8); System.out.println("接受到的客户端消息:" + request); // 响应内容: _ channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("这是服务端响应的消息", CharsetUtil.UTF_8)); } }

接下来开始写客户端的实现代码:

public static void main(String[] args) { _//__创建__nioEventLoopGroup _ NioEventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress("127.0.0.1", 8080)).handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ClientHandler()); } }); try { // _发起同步连接 _ ChannelFuture sync = bootstrap.connect().sync(); sync.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } finally { group.shutdownGracefully(); } }

ClientHandler类的实现

public class ClientHandler extends SimpleChannelInboundHandler<ByteBuf> { // _活跃通道可以发送消息 _ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // _发送数据 _ ctx.writeAndFlush(Unpooled.copiedBuffer("活跃通道发生消息?", CharsetUtil.UTF_8)); } protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception { System.out.println("接收服务端响应的信息:" + byteBuf.toString(CharsetUtil.UTF_8)); } }

然后就可以运行起来看效果了。

粘包和拆包的问题可以通过利用编码器LineBaseDFrameDecoder解决。在服务端的和客户端分别添加如下代码。

// 设置我们分割最大长度为__1024 socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024)); // 获取数据的结果为__string__类型 socketChannel.pipeline().addLast(new StringEncoder());

同时发送的消息加上一个\n字符进行区分。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

springboot整合netty实现TCP服务端

1、导入依赖_<!https://mvnrepository.com/artifact/io.netty/nettyall_<dependency\<groupId\io.netty</groupId\<artifactId\nettyall</artifact