使用 netty4 组件从 tcp 端口获取数据的问题
Issue in getting data from a tcp port using netty4 component
我正在使用 Camel Netty4 组件来侦听 TCP 端口上的数据。下面是我的代码:
public class TcpListener {
public static void main(String hh[]) throws Exception{
MyMessageDecoder byteDecoder = new MyMessageDecoder();
SimpleRegistry reg = new SimpleRegistry();
reg.put("decoder", byteDecoder);
CamelContext context = new DefaultCamelContext(reg);
context.addRoutes(new RouteBuilder() {
public void configure() {
from("netty4:tcp://0.0.0.0:5150?decoder=#decoder")
.to("file://C:/Users/Rahul/Desktop?fileName=tcpOutput.txt");
}
});
context.start();
}
}
class MyMessageDecoder extends ByteToMessageDecoder {
static FileWriter writer;
static {
try {
writer = new FileWriter("C:/Users/Rahul/Desktop/tcpOutputNew1.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> out) throws Exception {
if (buffer.readableBytes() < 1) {
return;
}
byte[] bytes = new byte[1];
buffer.readBytes(bytes);
MyMessage myMessage = new MyMessage(bytes);
System.out.println(bytes[0]);
System.out.println(Integer.toBinaryString(bytes[0]));
System.out.println(Integer.toHexString(bytes[0]));
System.out.println(myMessage);
out.add(myMessage);
}
}
class MyMessage {
protected byte data1;
public MyMessage(byte[] data) {
data1 = data[0];
}
public String toString() {
return "MyMessage: { " + this.data1 +" }";
}
}
在我的代码中,我尝试一次读取 1 个字节,因为在每个字节中我都会收到设备的 IMEI 编号。对于 IMEI 号码
351608084153316
我应该收到像
这样的数据
0x03 0x51 0x60 0x80 0x84 0x15 0x33 0x16
但是我收到的数据是
0x03 0x51 0x60 0xffffff80 0xffffff84 0x15 0x33 0x16.
我该如何解决这个问题,或者如何忽略这些附加了 IMEI 号码某些部分的不需要的字节。
这是正在发生的事情。
- 你有一个字节数组,在Java中被认为是有符号,即使你不关心符号位
当您尝试使用它们时,它们会自动 "promoted" 到 int
。由于如果最左边的位为 1,则 byte
和 int
都是有符号的,因此它向左传播,因此 int
值与 byte
数值相同。来自你的例子
0x03 0x51 0x60 0x80 0x84 0x15 0x33 0x16
第一个字节是0x03
或位模式0000_0011
。这是一个正值并被提升为 0x00000003
。 0x51
和 0x60
相同。 0x80 的情况有所不同。它的位模式是1000_0000,它的数值是-128
。当它提升时,它的符号被左扩展,导致 int
值 0xFFFFFF80
,它仍然具有数值 -128
.
实际上,none 这对您有影响,因为低位字节中的位模式没有改变。您的解决方案只是在您访问数组中的一个字节时屏蔽掉不需要的位。如
Integer.toHexString(0x000000FF & (int) bytes[0])
我正在使用 Camel Netty4 组件来侦听 TCP 端口上的数据。下面是我的代码:
public class TcpListener {
public static void main(String hh[]) throws Exception{
MyMessageDecoder byteDecoder = new MyMessageDecoder();
SimpleRegistry reg = new SimpleRegistry();
reg.put("decoder", byteDecoder);
CamelContext context = new DefaultCamelContext(reg);
context.addRoutes(new RouteBuilder() {
public void configure() {
from("netty4:tcp://0.0.0.0:5150?decoder=#decoder")
.to("file://C:/Users/Rahul/Desktop?fileName=tcpOutput.txt");
}
});
context.start();
}
}
class MyMessageDecoder extends ByteToMessageDecoder {
static FileWriter writer;
static {
try {
writer = new FileWriter("C:/Users/Rahul/Desktop/tcpOutputNew1.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> out) throws Exception {
if (buffer.readableBytes() < 1) {
return;
}
byte[] bytes = new byte[1];
buffer.readBytes(bytes);
MyMessage myMessage = new MyMessage(bytes);
System.out.println(bytes[0]);
System.out.println(Integer.toBinaryString(bytes[0]));
System.out.println(Integer.toHexString(bytes[0]));
System.out.println(myMessage);
out.add(myMessage);
}
}
class MyMessage {
protected byte data1;
public MyMessage(byte[] data) {
data1 = data[0];
}
public String toString() {
return "MyMessage: { " + this.data1 +" }";
}
}
在我的代码中,我尝试一次读取 1 个字节,因为在每个字节中我都会收到设备的 IMEI 编号。对于 IMEI 号码
351608084153316
我应该收到像
这样的数据0x03 0x51 0x60 0x80 0x84 0x15 0x33 0x16
但是我收到的数据是
0x03 0x51 0x60 0xffffff80 0xffffff84 0x15 0x33 0x16.
我该如何解决这个问题,或者如何忽略这些附加了 IMEI 号码某些部分的不需要的字节。
这是正在发生的事情。
- 你有一个字节数组,在Java中被认为是有符号,即使你不关心符号位
当您尝试使用它们时,它们会自动 "promoted" 到
int
。由于如果最左边的位为 1,则byte
和int
都是有符号的,因此它向左传播,因此int
值与byte
数值相同。来自你的例子0x03 0x51 0x60 0x80 0x84 0x15 0x33 0x16
第一个字节是
0x03
或位模式0000_0011
。这是一个正值并被提升为0x00000003
。0x51
和0x60
相同。 0x80 的情况有所不同。它的位模式是1000_0000,它的数值是-128
。当它提升时,它的符号被左扩展,导致int
值0xFFFFFF80
,它仍然具有数值-128
.实际上,none 这对您有影响,因为低位字节中的位模式没有改变。您的解决方案只是在您访问数组中的一个字节时屏蔽掉不需要的位。如
Integer.toHexString(0x000000FF & (int) bytes[0])