你好我写简单的tcp发送数据到netty服务器demo,这里是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();
我认为第一个readInt是25但是我得到419430400值
我想知道为什么?
答案 0 :(得分:2)
这似乎是一个大而小的endian错误。
大多数面向比特的协议都考虑了这个问题。不同的操作系统没有相同的字节序。所以你可以使用面向字符的协议。他们没有这个问题,但会传输更多字节。
示例:
长度为25的四个字节:19 00 00 00
Little-endian:00 00 00 19
,十进制:419430400
Big-endian:temp = a
a = b
b = temp
,十进制:25