简体   繁体   中英

send from tcp client to netty server data , How Bytebuf readInt work

Hello I write to simple tcp send data to netty server demo, Here is Code:

Client.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");
    }
}

Message.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 length = in.readInt();
The first readInt i think is 25 but i get 419430400 value
I want to know why??

It seems like a big- and little- endian bug.

Most of bit-oriented protocol consider this issue. Different operation system have not equal endianness. So you can use characters-oriented protocol. They do not have not this issue, but will transport more byte.

Example:

Length 25 in four bytes: 00 00 00 19

Little-endian: 19 00 00 00 , decimal: 419430400

Big-endian: 00 00 00 19 , decimal: 25

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM