简体   繁体   English

Java Socket Inputstream读取错误的数据

[英]Java Socket Inputstream Reading Wrong Data

Can anyone tell what wrong with this code, what I done is that 谁能告诉我这段代码有什么问题,我所做的是

  1. Client send image data which is uchar array from c++. 客户端发送图像数据,这是c ++的uchar数组。
  2. Java server receives this data using server socket and store as byte array. Java服务器使用服务器套接字接收此数据并存储为字节数组。

Here is the code... 这是代码...

   private ServerSocket serverSocket;
   InputStream in;
   int imageSize=300;//921600;//expected image size 640X480X3
   public imageReciver(int port) throws IOException {
      serverSocket = new ServerSocket(port);
    }


        Socket server = null;
        server = serverSocket.accept();
        in = server.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte buffer[] = new byte[100];

        int remainingBytes = imageSize; //
        while (remainingBytes > 0) {
        int bytesRead = in.read(buffer);
        if (bytesRead < 0) {
         throw new IOException("Unexpected end of data");
         }
        baos.write(buffer, 0, bytesRead);
        remainingBytes -= bytesRead;
        }
        in.close();

        byte imageByte[] = new byte[imageSize];         
        imageByte = baos.toByteArray();   
        baos.close();

While reading from inputstream in I am getting negative value on buffer . 在从inputstream in读取时inputstream in我在buffer上得到负值。

I think the problem is: 我认为问题是:

Java's byte is signed and you are sending unsigned bytes. Java的字节已签名,您正在发送未签名的字节。

From Java docs : Java文档

byte: The byte data type is an 8-bit signed two's complement integer . byte:byte数据类型是8位带符号的二进制补码整数 It has a minimum value of -128 and a maximum value of 127 (inclusive). 最小值为-128,最大值为127(含)。 The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. 字节数据类型对于在大数组中节省内存非常有用,因为内存节省实际上很重要。 They can also be used in place of int where their limits help to clarify your code; 在限制可以帮助您澄清代码的地方,也可以使用它们来代替int。 the fact that a variable's range is limited can serve as a form of documentation. 变量范围有限的事实可以作为文档的一种形式。

You can try something like: 您可以尝试如下操作:

short bVal = /*signed byte*/;
if (bVal < 0) 
   bVal = bVal + 256; //Convert to positive

Edit: As pointed out by @Aubin, a short will be a better choice instead of an int. 编辑:正如@Aubin所指出的,short会是一个更好的选择,而不是int。

The representation of a data as signed or unsigned is a human convention. 将数据表示为有符号或无符号是一种人类习惯。 This convention is used by the processor when arithmetic is used. 使用算术时,处理器使用此约定。

It's not the case here: it's simple read and write and the fact the bit 7 is used as negative flag or not as no effect. 事实并非如此:这是简单的读写操作,并且第7位用作否定标志或不起作用的事实。

Reading a socket and writing a file may be done with byte buffer or better with ByteBuffer . 读取套接字和写入文件可以使用字节缓冲区完成,也可以使用ByteBuffer更好。 I suggest a buffer size of 8K . 我建议缓冲区大小为8K

Rewriting the code using java.nio.channels may be better. 使用java.nio.channels重写代码可能更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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