Results 1 to 11 of 11

Thread: byte[]

  1. #1
    bobo67 is offline Member
    Join Date
    Sep 2010
    Posts
    37
    Rep Power
    0

    Question byte[]

    hi,
    I'm sending a byte[] from client to server. but the received byte[] isn't the same as with byte[] I had sent. Why this occurs?
    Regards,

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    bobo67 is offline Member
    Join Date
    Sep 2010
    Posts
    37
    Rep Power
    0

    Default

    I think it's different. could you please help me?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Can you do some debugging with your code to identify how the byte[] is not being sent properly? Are all the bytes sent being received? If 33 are sent are 33 bytes received? Is there one or more bytes that are received with different values than they were sent as?
    What is the value of the byte and what is its position in the array?

    For example the 17th byte was sent as a 0x83 and received as a 0x3F

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    post your code where the problem is happening

  6. #6
    bobo67 is offline Member
    Join Date
    Sep 2010
    Posts
    37
    Rep Power
    0

    Question

    here's the code
    Java Code:
    //client
    {
            Socket skt = new Socket("localhost", 4444);
    	System.out.print("Socket is open.");
    	OutputStream out = skt.getOutputStream();
    	DataOutputStream dout = new DataOutputStream(out);
    	dout.write([COLOR="Red"]M1e[/COLOR], 0, 48);
    	System.out.print(M1e+ "sent");
    	out.close();
    }
    //server
    {
    byte[] bytes = new byte[65536];
           Socket skt = srvr.accept();
            System.out.print("Server has connected!\n");
     
            InputStream ins = skt.getInputStream();
            DataInputStream din = new DataInputStream(ins);
            din.read([COLOR="Red"]bytes[/COLOR]);
            System.out.print("Received");
            System.out.println(bytes);
    }
    I don't know why M1e and bytes aren't equal.
    Last edited by bobo67; 09-11-2010 at 02:59 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    When posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]

  8. #8
    bobo67 is offline Member
    Join Date
    Sep 2010
    Posts
    37
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Are all the bytes sent being received? If 33 are sent are 33 bytes received? Is there one or more bytes that are received with different values than they were sent as?
    What is the value of the byte and what is its position in the array?
    I send [B@1eec612, and the length is 384.
    I receive [B@ca0b6, and the size is 384 too.
    Last edited by bobo67; 09-11-2010 at 02:25 PM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You are currently seeing the toString representation of an array, and while the two array addresses are different as to be expected, this tells you nothing about the array contents other than they are both arrays of bytes.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bobo67 View Post
    I send [B@1eec612, and the length is 384.
    I receive [B@ca0b6, and the size is size of my bytes variable which is 384.
    Compare the first 384 bytes of your receiving array with what was sent. And also please read the API documentation for the DataInputStream and DataOutputStream classes; your code shows that you don't know how they work.

    kind regards,

    Jos

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    M1e and bytes aren't equal.
    You have not shown how you know the bytes are not equal. See last part of post#4.
    There could be useful info if you do some more debugging.

Similar Threads

  1. Replies: 1
    Last Post: 04-21-2010, 03:28 AM
  2. Byte Ranges
    By alessandro_ in forum Networking
    Replies: 2
    Last Post: 02-14-2010, 12:48 PM
  3. Byte class ?
    By Hardik in forum New To Java
    Replies: 1
    Last Post: 07-20-2009, 04:34 PM
  4. Byte Array
    By sandor in forum New To Java
    Replies: 12
    Last Post: 01-15-2009, 04:31 AM
  5. int to byte
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 08:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •