client:
public class Client {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup workgroup = new NioEventLoopGroup();
final Bootstrap b = new Bootstrap();
b.group(workgroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
//这里如果用解码,就两边都收不到消息了
// ByteBuf buf = Unpooled.copiedBuffer("$".getBytes());
// socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,buf));
// socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture cf = b.connect("127.0.0.1",8888); //**问题出在这里,最后少了.syn()**
cf.channel().writeAndFlush(Unpooled.copiedBuffer("aa$".getBytes()));
cf.channel().writeAndFlush(Unpooled.copiedBuffer("bbbb$".getBytes()));
cf.channel().closeFuture().sync();//异步
workgroup.shutdownGracefully();
}
}
clientHandler:
public class ClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
// 读取Server的信息,只能用ButeBuf读取
ByteBuf buf = (ByteBuf) msg;
byte[] data = new byte[buf.readableBytes()];
buf.readBytes(data);
String request = new String(data, "utf-8");
System.out.println("服务端反馈的消息: " + request);
// String request = (String)msg;
// System.out.println("服务端反馈的消息: "+ request);
}finally {
ReferenceCountUtil.release(msg);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
Server:
public class Server {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
final ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
ByteBuf buf = Unpooled.copiedBuffer("$".getBytes());
socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,buf));
socketChannel.pipeline().addLast(new StringDecoder());
socketChannel.pipeline().addLast(new ServerHandler());
}
});
ChannelFuture future = b.bind(8888).sync();
future.channel().closeFuture().sync();
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
serverHandler:
public class ServerHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String str = (String)msg;
System.out.println("Server: "+str);
ctx.writeAndFlush(Unpooled.copiedBuffer("这是服务器响应$".getBytes()));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
现在的问题是我从client发送的信息在server端直接使用String,并且能按指定的字符串拆包
但是从服务端返回的信息在客户端上为什么不能直接解码成字符串?
netty是现在最新的版本,好像是4.1.15
server收到的消息:
Server: aa
Server: bbbb
client收到server反馈的消息:
服务端反馈的消息: 这是服务器响应$这是服务器响应$
我在Client端直接拆包也不行(就是两边都没响应)
ByteBuf buf = Unpooled.copiedBuffer("$".getBytes());
socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,buf));
然后单独解码没问题,看起来就是拆包的问题