5 Replies Latest reply: Sep 18, 2006 12:51 PM by 796447 RSS

    Read and write byte[]

    807569
      I have a client that that send a byte[] to the server. The server should then read the byte[] correctly:

      client:
      DataOutputStream outBytes = new DataOutputStream(socket.getOutputStream());
      
      String pp = "sally";
      byte[] ba = pp.getBytes()
      
      outBytes.write(ba);
      the server:
      DataInputStream bytesIn = new DataInputStream(socket.getInputStream());
      
      byte[] res = bytesIn.read(); //not allowed
      but the last line is not possible. How do I fetch the byte array from the client?
        • 1. Re: Read and write byte[]
          796447
          There is another read(byte[] buf, ...) method you need to invoke instead. The one you are invoking in the code you posted is for reading a single byte.
          • 2. Re: Read and write byte[]
            807569
            There is: read(byte[]b, int off, int b)

            but I don't know before hand how many bytes are send from the client....
            • 3. Re: Read and write byte[]
              796447
              There is: read(byte[]b, int off, int b)

              but I don't know before hand how many bytes are send
              from the client....
              Then you have a design problem. The sender should also send information about that. Usually one sends a "length" value, followed by the bytes. Then on the other side you of course first read the "length" so you know how many bytes there are to read after that.
              • 4. Re: Read and write byte[]
                807569
                I found the read and write methods in the BufferedOut/InputStream
                public int read(byte[] b,
                                int off,
                                int len)
                         throws IOException
                
                public void write(byte[] b,
                                  int off,
                                  int len)
                           throws IOException
                I have this on the client side:
                outBytes = new BufferedOutputStream(socket.getOutputStream());
                String pp = "sally";
                byte [] vv = pp.getBytes();
                int len = vv.length;
                outBytes.write(vv, 0, len);
                I have this on the server side:
                inBytes = new BufferedInputStream(socket.getInputStream());
                inBytes.read(b, off, len)
                but I am not sure how to obtain the arguments in the read call. Should I make an extra call from the client only specifying the length of the byte[]?

                when making: outBytes.write(vv, 0, len); the len is the correct value so it seems silly to send that separatly...
                • 5. Re: Read and write byte[]
                  796447
                  when making: outBytes.write(vv, 0, len); the len is the correct value so it seems silly to send that separatly...
                  You misunderstand what that argument is for. It is just to tell it how many bytes from the array to write. It does not also tell it to actually write the number itself too. If you need to know the number of bytes written (so that the receiver in your case can know how many bytes to read), then that value needs to be made available somehow. Writing the length before the bytes would be a way to do that.