Netty傳遞字符串


想在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>() {

			@Override
			protected 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>() {

			@Override
			protected 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());就好了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM