4 Replies Latest reply: Sep 18, 2006 1:06 PM by 807569 RSS

    Bytes received differs from those send!

    807569
      I have a client that sends some bytes:
      outBytes = new BufferedOutputStream(socket.getOutputStream());
                         
                         
                         
                         String pp = "sally";
                         byte[] b = pp.getBytes();
                         System.out.println("In bytes: " + b);
                         int len = b.length;
                         
                         outBytes.write(len);
                         outBytes.flush();
                         
                         outBytes.write(b, 0, len);
                         outBytes.flush();
      the server receives them with:
      inBytes = new BufferedInputStream(socket.getInputStream());
                         int len = inBytes.read();
                          byte[] b = new byte[len];
                         inBytes.read(b, 0, len);
                         
                         System.out.println(b + " bytes received!");
      but the bytes printed at the client side does not match the bytes printed at the server side. Any ideas why the get changed during the transmission?
        • 1. Re: Bytes received differs from those send!
          796447
          outBytes.write(len);
          This writes a single byte, which would definitely not represent a real int length of an array. Maybe that's where the trouble begins.
          System.out.println(b + " bytes received!");
          Also, this tries to print the array, not the length, so maybe you're confused at the output?
          • 2. Re: Bytes received differs from those send!
            807569
            have tried to print "len" at both sides and its 5. So the server also uses 5 as the len.

            when I print the bytes at the client I get:

            [B@179c285

            when I print them at the server I get:

            [B@7b7072

            the wierd thing is that if I convert the bytes at the server to a String I get "sally" eventhough the bytes received does not match the bytes send.
            • 3. Re: Bytes received differs from those send!
              796447
              Don't be confused by the B@179... thing. That's just Object's toString() method implementation, showing basically a memory address, not the contents of the array. If you want to "print" the array's contents, you'll need to do a little more work.

              Arrays.toString(b) might be what you want, if you have a recent-enough Java version.
              • 4. Re: Bytes received differs from those send!
                807569
                Just tried:
                                   String pp = "sally";
                                   byte[] b = pp.getBytes();
                                   for (int i = 0; i < b.length; i++)
                                   {
                                        System.out.println(b);
                               }

                at the client

                and:
                                   int len = inBytes.read();
                                    byte[] b = new byte[len];
                                   inBytes.read(b, 0, len);
                                   
                                   for (int i = 0; i < b.length; i++)
                                   {
                                        System.out.println(b);
                               }




                at the server.

                and in both cases it gave:

                115
                97
                108
                108
                121

                now I can rest :-) thanks for the help!