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

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 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀