Results 1 to 2 of 2
  1. #1
    sm123 is offline Member
    Join Date
    Mar 2010
    Posts
    11
    Rep Power
    0

    Default Problem reading from socket using read(bytes[])

    Hi,

    I am using Inputstream.read(bytes[]) to read bytes using TCP protocol.
    Problem is :

    When I try to run the code on windows server , it reads complete bytes.
    But when I run the same code on Linux server , I sometimes get incomplete bytes.

    I am trying to figure out why in.read() behaves differently on two platforms?

    Thanks,

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    read(bytes[]) doesn't guarantee you'll read it to the end. Here's how you write that code:

    Java Code:
    byte [] bytes = new byte [EXPECTED_SIZE];
    // read returns the number of bytes actually read
    int numRead = inputStream.read (bytes);
    
    // there're still more bytes to read:
    while (numRead != EXPECTED_SIZE) {
      // read more bytes and put them into the array starting at position "numRead",
      // for the maximum of "EXPECTED_SIZE - numRead" bytes
      numRead += inputStream.read (bytes, numRead, EXPECTED_SIZE - numRead);
    }

Similar Threads

  1. How to read first 9 bytes and write in...?
    By aRTx in forum New To Java
    Replies: 8
    Last Post: 03-26-2009, 02:54 PM
  2. Losing Packets/Bytes using DataInputStream and socket
    By ajordanneve in forum Networking
    Replies: 0
    Last Post: 03-24-2009, 05:22 AM
  3. Replies: 5
    Last Post: 10-17-2008, 02:13 PM
  4. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 03:46 PM
  5. Reading bytes from InputStream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-25-2007, 07:51 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
  •