netty中channelRead为什么没有执行?
服务器端:
public class HelloServer {
public void start(int port)throws Exception{
EventLoopGroup bossGroup=new NioEventLoopGroup();
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
ServerBootstrap b=new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
// 注册两个OutboundHandler,执行顺序为注册顺序的逆序,所以应该是OutboundHandler2 OutboundHandler1
ch.pipeline().addLast(new OutboundHandler1());
ch.pipeline().addLast(new OutboundHandler2());
// 注册两个InboundHandler,执行顺序为注册顺序,所以应该是InboundHandler1 InboundHandler2
ch.pipeline().addLast(new InboundHandler1());
ch.pipeline().addLast(new InboundHandler2());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f=b.bind(port).sync();
System.out.println("Server:start to connect....");
f.channel().closeFuture().sync();
}finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception{
HelloServer server=new HelloServer();
server.start(8000);
}
}
public class InboundHandler1 extends ChannelInboundHandlerAdapter{
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler1.channelActive");
ctx.fireChannelActive();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("InboundHandler1.channelRead");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler1.channelReadComplete");
ctx.flush();
}
}
public class InboundHandler2 extends ChannelInboundHandlerAdapter{
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler2.channelActive");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("InboundHandler2.channelRead");
ByteBuf result=(ByteBuf)msg;
byte[] result1=new byte[result.readableBytes()];
result.readBytes(result1);
String resultStr=new String(result1);
System.out.println("Client said:"+resultStr);
result.release();
ctx.write(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler2.channelReadComplete");
ctx.flush();
}
}
public class OutboundHandler1 extends ChannelOutboundHandlerAdapter{
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
System.out.println("OutboundHandler1.write");
String response="I am ok";
ByteBuf encoded=ctx.alloc().buffer(4*response.length());
encoded.writeBytes(response.getBytes());
ctx.write(encoded);
ctx.flush();
}
}
public class OutboundHandler2 extends ChannelOutboundHandlerAdapter{
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
System.out.println("OutboundHandler2.write");
}
}
客户端:
public class HelloClient {
public void connect(String host,int port)throws Exception{
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
Bootstrap b=new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
ch.pipeline().addLast(new HelloClientIntHandler());
}
});
System.out.println("Client:start to connect...");
ChannelFuture f=b.connect(host,port).sync();
f.channel().closeFuture().sync();
}finally{
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args)throws Exception {
HelloClient client=new HelloClient();
client.connect("127.0.0.1", 8000);
}
}
public class HelloClientIntHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("HelloClientIntHandler.channelRead");
ByteBuf result=(ByteBuf)msg;
byte[] result1=new byte[result.readableBytes()];
result.readBytes(result1);
result.release();
ctx.close();
System.out.println("Server said:"+new String(result1));
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("HelloClientIntHandler.channelActive");
String msg="Are you ok?";
ByteBuf encoded=ctx.alloc().buffer(4* msg.length());
ctx.write(msg.getBytes());
ctx.write(encoded);
ctx.flush();
}
}
public class HelloServer {
public void start(int port)throws Exception{
EventLoopGroup bossGroup=new NioEventLoopGroup();
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
ServerBootstrap b=new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
// 注册两个OutboundHandler,执行顺序为注册顺序的逆序,所以应该是OutboundHandler2 OutboundHandler1
ch.pipeline().addLast(new OutboundHandler1());
ch.pipeline().addLast(new OutboundHandler2());
// 注册两个InboundHandler,执行顺序为注册顺序,所以应该是InboundHandler1 InboundHandler2
ch.pipeline().addLast(new InboundHandler1());
ch.pipeline().addLast(new InboundHandler2());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f=b.bind(port).sync();
System.out.println("Server:start to connect....");
f.channel().closeFuture().sync();
}finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception{
HelloServer server=new HelloServer();
server.start(8000);
}
}
public class InboundHandler1 extends ChannelInboundHandlerAdapter{
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler1.channelActive");
ctx.fireChannelActive();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("InboundHandler1.channelRead");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler1.channelReadComplete");
ctx.flush();
}
}
public class InboundHandler2 extends ChannelInboundHandlerAdapter{
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler2.channelActive");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("InboundHandler2.channelRead");
ByteBuf result=(ByteBuf)msg;
byte[] result1=new byte[result.readableBytes()];
result.readBytes(result1);
String resultStr=new String(result1);
System.out.println("Client said:"+resultStr);
result.release();
ctx.write(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println("InboundHandler2.channelReadComplete");
ctx.flush();
}
}
public class OutboundHandler1 extends ChannelOutboundHandlerAdapter{
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
System.out.println("OutboundHandler1.write");
String response="I am ok";
ByteBuf encoded=ctx.alloc().buffer(4*response.length());
encoded.writeBytes(response.getBytes());
ctx.write(encoded);
ctx.flush();
}
}
public class OutboundHandler2 extends ChannelOutboundHandlerAdapter{
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
System.out.println("OutboundHandler2.write");
}
}
客户端:
public class HelloClient {
public void connect(String host,int port)throws Exception{
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
Bootstrap b=new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
ch.pipeline().addLast(new HelloClientIntHandler());
}
});
System.out.println("Client:start to connect...");
ChannelFuture f=b.connect(host,port).sync();
f.channel().closeFuture().sync();
}finally{
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args)throws Exception {
HelloClient client=new HelloClient();
client.connect("127.0.0.1", 8000);
}
}
public class HelloClientIntHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("HelloClientIntHandler.channelRead");
ByteBuf result=(ByteBuf)msg;
byte[] result1=new byte[result.readableBytes()];
result.readBytes(result1);
result.release();
ctx.close();
System.out.println("Server said:"+new String(result1));
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("HelloClientIntHandler.channelActive");
String msg="Are you ok?";
ByteBuf encoded=ctx.alloc().buffer(4* msg.length());
ctx.write(msg.getBytes());
ctx.write(encoded);
ctx.flush();
}
}
...全文
相关推荐
Netty同步等待数据返回实例代码
Netty同步等待数据返回实例代码
Idea+springboot+netty打包执行测试
Idea+springboot+netty打包执行测试,打成的jar包含依赖包。执行的时候,请在jar包所在的目录下执行。执行命令为:java -jar xxx.jar
用Netty实现Websocket(包含服务器代码和客户端网页)
用Netty实现Websocket(包含服务器代码和客户端网页)
netty4-netty5.rar
ChannelHandlerAdapter 4.X版本和5.X版本的差别很大。ChannelRead是属于5.X版本的4.X版本没有这个方法,所以如果要用ChannelRead。可以更换5.X版本的Netty。
spring+netty+mybatis整合实例
spring+netty+mybatis整合实例 实现一个简单的socket服务,启动服务后,客户端发送一条消息,服务端从数据库查询数据并返回给客户端。


