为什么我的某些套接字数据被 "3f" 字节覆盖?
Why is some of my socket data getting overwritten with "3f" bytes?
我使用的是:Android Studio/Java 1.8(客户端)VS2010/.NET 4.0(服务器)
此Java代码不断发送无效数据:
long FileSize = 1131666;
byte [] fs = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(FileSize).array();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write("FILE".getBytes());
baos.write(fs); //HERE
baos.write(GetHash().getHexStdstring().getBytes());
baos.write(dest_filename.getBytes());
OutputStream out = socket.getOutputStream();
out.write(baos.toByteArray());
out.flush();
这就是我得到的(用wireshark查看):
46:49:4c:45: ("FILE")
3f:44:11:00:00:00:00:00: (1131583)
32:37:64:35:36:61:32:34:32:36:31:30:37:36:37:32:30:65:34:38:66:37:34:65:36:61:64:38:34:65:36:30:65:64:33:63:66:64:34:36:32:64:36:62:37:65:62:64:62:32:63:63:62:37:37:64:36:38:37:66:64:64:66:39:
5c:45:75:72:6f:70:65:2e:70:6e:67
因此,我的C++服务器应用程序接收的值为1131583
,但无论我使用什么值作为FileSize
,我似乎总是在某个地方得到3f
。。。
另一个例子是当FileSize为22451663
时,我得到3f:3f:56:01:00:00:00:00
(或22429503
)。
有什么想法吗?
奇怪的是,如果我将fs
翻译回一个数字或字符串,并在发送之前Toast该值,它就会说它是正确的。
6:49:4c:45: ("FILE")
00:00:00:00:00:11:44:3f: (1131583)
无法复制,并且00:00:00:00:00:11:44:3f:
不是1131583的小端表示。它是1131583的big-endian表示。作为一个小的endian数,它是一个相当大的数:4347598471168。
打印的该代码在电线上实际产生的内容
ByteArrayOutputStream baos = new ByteArrayOutputStream()
{
public void write(byte[] bytes) throws IOException
{
for (byte b : bytes)
{
System.out.print(Integer.toHexString(b & 0xff)+":");
}
System.out.println();
super.write(bytes);
}
};
是:
46:49:4c:45:
92:44:11:0:0:0:0:0:
// etc.
此代码没有问题。
因此,0x3f
的ISO-8859-1字符是?
,而且似乎因为我将构建的ByteArayOutputStream发送到Send(String string)
函数,所以它将大量字节更改为0x3f
,就像0x92
没有ISO-8859-1字符一样。
我把我的代码改成这样:
// in main code
Bytestring message = new Bytestring(baos.toByteArray());
Send(message);
// send function
public void Send(Bytestring string) {
OutputStream out = socket.getOutputStream();
// other code for writing header
out.write(string.getBytes());
out.flush();
}
Bytestring是我基于字节数组创建的字符串类,而不是基于(2字节)char数组的string。
相关文章:
- 从不同线程使用int64的不同字节安全吗
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 在UNIX系统中使用DIR查找文件的字节大小
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- std::当在256字节边界上写入整数时,流的奇怪行为
- 当比特(而不是字节)的顺序至关重要时的持久性
- 从文件中读取多个字节,并将它们存储在C++中进行比较
- 如何在文件中查找字节序列
- luaL_dofile在已知良好的字节码上失败,可以使用未编译的版本
- 字节到位运算符重载C++
- 在java中读取c++字节的位字段
- 使用 std::vector::reverse_iterator 将 int 序列化为字节向量?
- 字节真的是最小可寻址单元吗
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- asn1c 不会从 asn.1 模块中提取八位字节字符串的默认值
- 如何将原始字节附加到 std::vector?
- 为什么我的某些套接字数据被 "3f" 字节覆盖?