Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem you have is that you don't currently have any way to say when one byte array ends and the next one starts. (In your "one array" solution, the end of the byte array corresponds to the end of stream. And of course, once the stream has been ended / closed, it cannot be reopened without creating a new <code>Socket</code>, etcetera.)</p> <p>The simple way to solve this is as follows, using DataOutputStream and DataInputStream pairs wrapped around the respective socket streams:</p> <ul> <li><p>To send a byte array:</p> <ol> <li><p>Convert data to bytes.</p></li> <li><p>Send the byte array size using the <code>DataOutputStream.writeInt(int)</code> method.</p></li> <li><p>Send the byte array using <code>DataOutputStream.write(byte[])</code> method.</p></li> </ol></li> <li><p>To receive a byte array:</p> <ol> <li><p>Receive the byte array size using the <code>DataInputStream.readInt()</code> method.</p></li> <li><p>Allocate a byte array of the required size.</p></li> <li><p>Receive the bytes into the byte array using the <code>DataInputStream.read(byte[], int, int)</code> method ... repeatedly until you've gotten all of the bytes.</p></li> </ol></li> </ul> <p>By sending the size of the byte array at the front, you tell the receiver how many bytes to read. You can repeat this process as many times as you need. The sender can indicate to the receiver that there are no more byte arrays to send by simply closing the socket stream.</p> <p>Note - this is pseudo-code. I assume that you are capable of turning it into working Java.</p> <p>Don't forget to insert BufferedInputStreams and BufferedOutputStreams into the respective stream chains ... to reduce system call overheads.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload