分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

Netty传递字符串

发布时间:2023-09-06 02:04责任编辑:苏小强关键词:暂无标签

想在Netty的channel中传递字符串,需要在客户端Client设置sc.pipeline().addLast(new StringEncoder());服务端Server设置sc.pipeline().addLast(new StringDecoder());就可以了;

客户端代码:

package com.netty.client;import io.netty.bootstrap.Bootstrap;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.codec.string.StringEncoder;public class Client {public static void main(String[] args) throws InterruptedException {EventLoopGroup workgroup = new NioEventLoopGroup();Bootstrap b = new Bootstrap();b.group(workgroup);b.channel(NioSocketChannel.class);b.handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel sc) throws Exception {sc.pipeline().addLast(new StringEncoder());sc.pipeline().addLast(new ClientHandler());}});ChannelFuture future = b.connect("127.0.0.1", 8080).sync();future.channel().writeAndFlush("asda");future.channel().closeFuture().sync();workgroup.shutdownGracefully();}}

  服务端代码:

package com.netty.server;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.codec.string.StringDecoder;import io.netty.handler.logging.LogLevel;import io.netty.handler.logging.LoggingHandler;public class Server {public static void main(String[] args) throws InterruptedException {EventLoopGroup bossgroup = new NioEventLoopGroup();EventLoopGroup workgroup = new NioEventLoopGroup();ServerBootstrap sb = new ServerBootstrap();sb.channel(NioServerSocketChannel.class);sb.group(bossgroup,workgroup);sb.handler(new LoggingHandler(LogLevel.INFO));sb.childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel sc) throws Exception {sc.pipeline().addLast(new StringDecoder());sc.pipeline().addLast(new ServerHandler());}});ChannelFuture future = sb.bind(8080).sync();future.channel().closeFuture().sync();workgroup.shutdownGracefully();bossgroup.shutdownGracefully();}}

  

package com.netty.server;import java.nio.charset.Charset;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;import io.netty.util.CharsetUtil;import io.netty.util.ReferenceCountUtil;public class ServerHandler extends ChannelHandlerAdapter { ???@Override ???public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ???????try {
        //这里可以直接获取str ???????????String str = (String)msg; ???????????System.out.println(str); ???????} finally { ???????????ReferenceCountUtil.release(msg); ???????} ????} ???@Override ???public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ???}}

那么如果服务端要给客户端也返回String类型的,只需要在服务端和客户端都加上sc.pipeline().addLast(new StringEncoder());sc.pipeline().addLast(new StringDecoder());就好了。

Netty传递字符串

原文地址:https://www.cnblogs.com/TravisGrady/p/9337418.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved