Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Netty客户端无法从非Netty服务器读取响应_Java_Sockets_Tcp_Netty_Tcp Ip - Fatal编程技术网

Java Netty客户端无法从非Netty服务器读取响应

Java Netty客户端无法从非Netty服务器读取响应,java,sockets,tcp,netty,tcp-ip,Java,Sockets,Tcp,Netty,Tcp Ip,我有一个Tcp客户机,它连接到一个旧的大型机(52年),从中发送和接收请求和响应 这是我的客户机的核心连接部分 public class SimpleConnector { private String carrier; private SocketChannel socketChannel; public static final byte END_OF_MESSAGE_BYTE = (byte) 0x2b; public SimpleConnector(S

我有一个Tcp客户机,它连接到一个旧的大型机(52年),从中发送和接收请求和响应

这是我的客户机的核心连接部分

public class SimpleConnector {

    private String carrier;
    private SocketChannel socketChannel;
    public static final byte END_OF_MESSAGE_BYTE = (byte) 0x2b;

    public SimpleConnector(String carrier, InetSocketAddress inetSocketAddress) throws IOException {
        this.carrier = this.carrier;
        socketChannel = SocketChannel.open();
        socketChannel.socket().connect(inetSocketAddress, 30000);
    }

    public void shutDown() throws IOException {
        this.socketChannel.close();
    }
    //Send Request
    public String sendRequest(String request) throws Exception {
            final CharsetEncoder charsetEncoder = Charset.forName("ISO-8859-1").newEncoder();
            int requestLength = 12 + request.length() + 1;
            ByteBuffer buffer = ByteBuffer.allocate(requestLength);
            buffer.order(ByteOrder.BIG_ENDIAN);
            buffer.putInt(requestLength);
            buffer.put(charsetEncoder.encode(CharBuffer.wrap(carrier)));
            buffer.put(charsetEncoder.encode(CharBuffer.wrap(request)));
            buffer.put(END_OF_MESSAGE_BYTE);
            buffer.flip();
            socketChannel.write(buffer);
            return readResponse();

    }
    //Read Response
    protected String readResponse() throws Exception {
            CharsetDecoder charsetDecoder = Charset.forName("ISO-8859-1").newDecoder();
            int responseHeaderLength = 12;
            ByteBuffer responseHeaderBuf = ByteBuffer.allocate(responseHeaderLength);
            responseHeaderBuf.order(ByteOrder.BIG_ENDIAN);
            int bytesRead = 0;
            do {
                bytesRead = socketChannel.read(responseHeaderBuf);
            } while (bytesRead!=-1 && responseHeaderBuf.position()<responseHeaderLength);

            if (bytesRead==-1) {
                throw new IOException(carrier + " : Remote connection closed unexpectedly");
            }
            responseHeaderBuf.flip();
            int lengthField = responseHeaderBuf.getInt();
            int responseLength = lengthField - responseHeaderLength;
            responseHeaderBuf.clear();
            ByteBuffer responseBuf = ByteBuffer.allocate(responseLength);
            bytesRead = socketChannel.read(responseBuf);
            if (bytesRead>responseBuf.limit() || bytesRead ==-1) {
                throw new IOException(carrier + " : Remote connection closed unexpectedly");
            }
            responseBuf.flip();
            if (responseBuf.get(responseBuf.limit()-1)==END_OF_MESSAGE_BYTE) {
                responseBuf.limit(responseBuf.limit()-1);
            }
            responseBuf.clear();
            String response = charsetDecoder.decode(responseBuf).toString();
            return response;

    }

    public static void main(String[] args) throws Exception{
        SimpleConnector simpleConnector = new SimpleConnector("carrier",new InetSocketAddress("localhost",9999));
        String response=simpleConnector.sendRequest("Request");
        System.out.println(response);
    }
}
处理者

public class NettyClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        super.channelReadComplete(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("NettyClientHandler : channelRead" );
        ByteBuf byteBuf = (ByteBuf) msg;
        String message = byteBuf.toString(Charset.defaultCharset());
        System.out.println("Received Message : " + message);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        System.out.println("NettyClientHandler : channelActive" );
    }
}
我最初认为netty将只与netty服务器一起工作,但这个答案消除了我对此的疑虑


有人能告诉我,我做错了什么吗?

我想问题出在你的客户经理身上。在tcp服务器和客户端之间建立连接时,应调用
channelActive
方法中的
writeAndFlush()
。请使用下面更新的代码,看看它是否解决了问题

    @Sharable
    public class NettyClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

        @Override
        public void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
            String message = byteBuf.toString(Charset.defaultCharset());
            System.out.println("Received Message : " + message);
        }

        @Override
        public void channelActive(ChannelHandlerContext channelHandlerContext){
            channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("Netty Rocks!", CharsetUtil.UTF_8));
        }

    }
@Sharable
公共类NetCyclientHandler扩展了SimpleChannelInboundHandler{
@凌驾
public void channelRead0(ChannelHandlerContext ctx,ByteBuf ByteBuf)引发异常{
String message=byteBuf.toString(Charset.defaultCharset());
System.out.println(“接收到的消息:+消息”);
}
@凌驾
public void channelActive(ChannelHandlerContext ChannelHandlerContext){
channelHandlerContext.writeAndFlush(unmooled.copiedBuffer(“nettyrocks!”,CharsetUtil.UTF_8));
}
}

能否请您提供获得的异常的详细信息。我使用了“baeldung”教程中客户端中提到的classed。没有获得任何异常我的响应解码器不会触发readHard,如果没有看到完整的代码,很难说,但我怀疑您将处理程序放入管道的顺序是问题所在。你能交换客户端处理程序和解码器,使解码器在客户端处理程序的“前面”吗?@NormanMaurer尝试了这个方法,没有工作我更新了代码hi@NormanMaurer我更新了客户端代码谢谢你的回答,但这不是我想要的,我需要根据收到的响应执行多条消息。类似的东西
    @Sharable
    public class NettyClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

        @Override
        public void channelRead0(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
            String message = byteBuf.toString(Charset.defaultCharset());
            System.out.println("Received Message : " + message);
        }

        @Override
        public void channelActive(ChannelHandlerContext channelHandlerContext){
            channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("Netty Rocks!", CharsetUtil.UTF_8));
        }

    }