java - 从tcp客户端发送数据到netty服务器,Bytebuf readInt如何工作

标签 java tcp netty

你好,我写了一个简单的tcp发送数据到netty服务器演示,这里是代码:

客户端.java

package chat;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {

    private Socket socket;
    private InputStream inputStream;
    private OutputStream outputStream;
    private ByteArrayOutputStream byteArrayOutputStream;

    public Client() {
        try {
            socket = new Socket("127.0.0.1", 8081);
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
            byteArrayOutputStream = new ByteArrayOutputStream(100);
            System.out.println("Start Client...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendMessage(String context) throws IOException {
       Message message = new Message(context);

        pushDataToStream(message.getLength());
        pushDataToStream(message.getCode());
        pushDataToStream(message.getMagic());
        byteArrayOutputStream.write(context.getBytes());
        pushDataToStream(message.getEnd());
        System.out.println(byteArrayOutputStream.size());
        outputStream.write(byteArrayOutputStream.toByteArray());

        byteArrayOutputStream.reset();
        outputStream.flush();
    }

    private void pushDataToStream(int[] data) {
        if (data != null) {
            for (int i : data) {
                byteArrayOutputStream.write(i);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        Client client = new Client();
        client.sendMessage("HELLO, WORLD");
    }
}

消息.java

package chat;

import java.util.Arrays;

public class Message {

    private int[] length;
    private int[] code;
    private int[] magic;
    private String context;
    private int[] end;

    public Message(String context) {
        length = new int[]{calculateLength(context), 0x00, 0x00, 0x00};
        code = new int[]{calculateLength(context), 0x00, 0x00, 0x00};
        magic = new int[]{0xb1, 0x02,0x00, 0x00};
        this.context = context;
        end = new int[]{0x00};
    }

    public int calculateLength(String context) {
        return 4 + 4 + 4 + context.length() + 1;
    }

    public int[] getLength() {
        return length;
    }

    public void setLength(int[] length) {
        this.length = length;
    }

    public int[] getCode() {
        return code;
    }

    public void setCode(int[] code) {
        this.code = code;
    }

    public int[] getMagic() {
        return magic;
    }

    public void setMagic(int[] magic) {
        this.magic = magic;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    public int[] getEnd() {
        return end;
    }

    public void setEnd(int[] end) {
        this.end = end;
    }

    @Override
    public String toString() {
        return "Message{" +
                "length=" + Arrays.toString(length) +
                ", code=" + Arrays.toString(code) +
                ", magic=" + Arrays.toString(magic) +
                ", context='" + context + '\'' +
                ", end=" + Arrays.toString(end) +
                '}';
    }
}

NettyServer.java

package chat;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

import java.util.List;


public class NettyServer {

    private NioEventLoopGroup bossLoopGroup;
    private NioEventLoopGroup workerLoopGroup;
    private ServerBootstrap bootstrap;

    private NettyServer() {
        bossLoopGroup = new NioEventLoopGroup();
        workerLoopGroup = new NioEventLoopGroup();
        bootstrap = new ServerBootstrap();
    }

    public void run() throws Exception {
        bootstrap.group(bossLoopGroup, workerLoopGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024)
                .option(ChannelOption.TCP_NODELAY, true)
                .option(ChannelOption.SO_KEEPALIVE, true)
                .childHandler(new NettyServerHandler());

        ChannelFuture future = bootstrap.bind(8081).sync();
        if (future.isSuccess()) {
            System.out.println("Server start..");
        } else {
            System.exit(-1);
        }
    }

    public class NettyServerHandler extends ChannelInitializer<SocketChannel> {
        @Override
        protected void initChannel(SocketChannel socketChannel) throws Exception {
            ChannelPipeline pipeline = socketChannel.pipeline();
            pipeline.addLast(new NettySimpleDecoder());
            pipeline.addLast(new NettyServerProcess());
        }
    }

    public class NettyServerProcess extends SimpleChannelInboundHandler{

        @Override
        protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        }
    }

    public class NettySimpleDecoder extends ByteToMessageDecoder {

        @Override
        protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list) throws Exception {
            if (in.readableBytes() < 4) {
                return;
            }

            int length = in.readInt();
            //The first readInt i think is 25 but i get 419430400 value
            //i want to know why??
            System.out.println("length : " + length);
        }
    }

    public static void main(String[] args) throws  Exception {
        NettyServer nettyServer = new NettyServer();
        nettyServer.run();
    }


}


+-------+--------+------+----------------+--------+
| Length| Code   | Magic| Actual Content | End    |
| 0x000C| 0x000C | 0xb1 | "HELLO, WORLD" | 0x00   |
+-------+--------+------+----------------+--------+

int 长度 = in.readInt();
我认为第一个 readInt 是 25 但我得到 419430400 值
我想知道为什么??

最佳答案

看起来像是一个大-和小-endian错误。

大多数面向位的协议(protocol)都考虑这个问题。不同操作系统的字节序并不相同。所以你可以使用面向字符的协议(protocol)。他们没有这个问题,但会传输更多字节。

示例:

长度 25,四个字节:00 00 00 19

小尾数:19 00 00 00,十进制:419430400

大尾数:00 00 00 19,十进制:25

关于java - 从tcp客户端发送数据到netty服务器,Bytebuf readInt如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34835702/

相关文章:

asynchronous - Netty ServerBootstrap - 异步绑定(bind)?

java - testng 在一个线程中运行所有测试类

java - 在多线程环境中使用什么; vector 或数组列表

Java Threadpool TCP 服务器(端口不断变化!!)

http - 服务器崩溃 = 在此之后 TCP/IP(和浏览器客户端)的行为如何?

java - Netty websocket SSL 连接 HANDSHAKE_ISSUED

netty - 当请求被写入代理 netty 服务器中的 outboundChannel 时,如何在同一个处理程序中获取响应 byteBuf

java - Delphi转java、soap delphi、短信发送

就餐哲学家的Monitor类中的java.lang.IllegalMonitorStateException

c - 套接字 TCP : Why do I lose messages with scanf