Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的一些套接字数据会被“覆盖”;3f“;字节?_Java_Android_C++_Sockets - Fatal编程技术网

Java 为什么我的一些套接字数据会被“覆盖”;3f“;字节?

Java 为什么我的一些套接字数据会被“覆盖”;3f“;字节?,java,android,c++,sockets,Java,Android,C++,Sockets,我使用的是:Android Studio/Java1.8(客户端)VS2010/.NET4.0(服务器) 此Java代码不断发送无效数据: long FileSize = 1131666; byte [] fs = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(FileSize).array(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ba

我使用的是:Android Studio/Java1.8(客户端)VS2010/.NET4.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查看):

<我的C++服务器应用程序收到的值是<代码> 1131583代码>代码,但是不管我用什么值作为代码>文件化< /代码>,我总是在某处得到<代码> 3F <代码>…p> 另一个例子是当文件大小为
22451663
时,我得到
3f:3f:56:01:00:00:00
(或
22429503

有什么想法吗

奇怪的是,如果我将
fs
翻译回一个数字或字符串,并在发送之前将值烤熟,它会说它是正确的

6:49:4c:45: ("FILE")
00:00:00:00:00:11:44:3f: (1131583)
无法复制,并且
00:00:00:00:11:44:3f:
不是1131583的一个小端点表示。它是1131583的大端表示。作为一个小的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);
        }
    };
是:


这段代码没有问题。

因此,
0x3f
的ISO-8859-1字符是
,似乎因为我正在将我构造的BytearyOutputStream发送到我的
发送(字符串)
函数,它将大量字节更改为
0x3f
,类似于没有ISO-8859-1字符的
0x92

我将代码更改为这样:

// 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();
}

ByTstring是基于字节数组而不是基于(2字节)字符数组的字符串创建的字符串类。< /P>如果这是java,为什么它被标记为C++?为什么<代码> BytErayOuttoStruts?把它写在插座上就行了。如果需要缓冲,请在其中放置一个

BufferedOutputStream
。@soulsabr-因为我的服务器应用程序是用c++@EJP编写的,因为我实际上有一个方法Send()来完成所有这些,我只是压缩了一个小问题的代码是的,你是对的,对不起,我忘了我将其更改为big-endian以查看是否有任何更改。
46:49:4c:45:
92:44:11:0:0:0:0:0:
// etc.
// 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();
}