shanhm1991 2017-11-26 03:19 采纳率: 0%
浏览 10114

netty 开发问题,客户端发了请求后收不到响应

package org.netty_client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Client {

public void connect(String host,int port) {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap strap = new Bootstrap();
        strap.group(group);
        strap.channel(NioSocketChannel.class);
        strap.option(ChannelOption.TCP_NODELAY, true);
        strap.handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel arg0) throws Exception {
                arg0.pipeline().addLast(new ClientHandler());
            }
        });
        ChannelFuture future = strap.connect(host, port).sync();
        future.channel().closeFuture().sync();
        System.out.println("close");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }finally {
        group.shutdownGracefully();
    }
}

public static void main(String[] args) {
    new Client().connect("127.0.0.1", 8080); 
}

}

package org.netty_client;

import java.io.UnsupportedEncodingException;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ClientHandler extends ChannelHandlerAdapter{

private ByteBuf buf;

public ClientHandler() {
    String r = "first request";
    byte[] req = r.getBytes();
    buf = Unpooled.buffer(req.length);
    buf.writeBytes(req);
    System.out.println("send request:" + r);
}

public void channelActive(ChannelHandlerContext ctx) {
    ctx.writeAndFlush(buf);
}

public void channelkRead(ChannelHandlerContext ctx,Object msg) throws UnsupportedEncodingException {
    System.out.println(1);
    ByteBuf buf = (ByteBuf)msg;
    byte[] req = new byte[buf.readableBytes()];
    buf.readBytes(req);
    String body = new String(req,"UTF-8");
    System.out.println("receive response:" + body); 
}

public void exceptionCaught(ChannelHandlerContext ctx,Throwable e) {
    e.printStackTrace();
    ctx.close();
}

}

package org.netty_server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class Server {

public void bind(int port) {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap server = new ServerBootstrap();
        server.group(bossGroup,workerGroup);
        server.channel(NioServerSocketChannel.class);
        server.option(ChannelOption.SO_BACKLOG, 1024);
        server.childHandler(new ChannelInitializer<SocketChannel>(){
            @Override
            protected void initChannel(SocketChannel arg0) throws Exception {
                arg0.pipeline().addLast(new ServerHandler());
            }
        }); 
        ChannelFuture future = server.bind(port).sync();
        System.out.println("启动服务端口:" + port); 
        future.channel().closeFuture().sync();
        System.out.println("close");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

public static void main(String[] args) {
    new Server().bind(8080); 
}

}

package org.netty_server;

import java.io.UnsupportedEncodingException;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class ServerHandler extends ChannelHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx,Object msg) throws UnsupportedEncodingException {
ByteBuf buf = (ByteBuf)msg;
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
String body = new String(req,"UTF-8");
System.out.println("receive request:" + body);

    String r = "response";
    byte[] re = r.getBytes();
    ByteBuf resp = Unpooled.copiedBuffer(re);

// resp.readBytes(re);
System.out.println("send response:" + r);
ctx.write(resp);
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
    ctx.flush();
}

public void exceptionCaught(ChannelHandlerContext ctx,Throwable e) {
    e.printStackTrace();
    ctx.close();
}

}

测试结果:
send request:first request

启动服务端口:8080
receive request:first request
send response:response

但是客户端没收到响应,有朋友能看下么,只是写个简单的例子而已

  • 写回答

2条回答 默认 最新

  • qq_31290353 2017-12-21 08:03
    关注

    clientHandler里面写的有问题,override没有,所以没有识别

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    System.out.println(1);
    ByteBuf buf = (ByteBuf)msg;
    byte[] req = new byte[buf.readableBytes()];
    buf.readBytes(req);
    String body = new String(req,"UTF-8");
    System.out.println("receive response:" + body);
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。