0
votes

I have a big byte array of length more than 1200000. I want to send it by DataOutputStream, and receive at client by DataInputStream.

I'm using the code

    out.write(outData)

    in.readFully(inData)

out is DataOutputStream, in is DataInputStream, outData is the byte array I want to send. When I run the program, if the length of byte array is around 120000, the array can be sent, but when the length becomes 1200000, the server cant receive the array. Should I split the big array into some small ones?

I tried such code below, but it still not working.

        out.writeInt(outData.length);

        int start = 0;
        int len = 0;
        int count = outData.length;

        while (count > 0) {
            if (count < 4096) 
                len = count;
            else len = 4096;

            out.write(outData, start, len);
            start += len;
            count -= len;
        }

and

        int length=in.readInt();
        byte[] inData=new byte[length];
        in.readFully(inData);

Can somebody help? Thanks.

1
You can send as big as you want using socket.write() don't have to cache it yourself Java does that for you.. But receiving is a different story it will come in at random you have to have some kind of identifier to cut the packets yourself or else it will just be a long never ending packet. - SSpoke
No, you shouldn't have to do that. Post the code, and elaborate on "the server cant receive the array". Tell us precisely what happens. - JB Nizet
What protocol are you using? TCP or UDP? - Willem Van Onsem
You could frame the bytes with {SOH}[message bytes]{ETX}, and on the receive side read into a buffer starting at SOH -> ETX, stopping then to construct your objects from the recieved bytes. - Mark W
I'm using TCP protocol, is it not ok i use in.readFully(inData) to cut the message at receiver? - jacobbb

1 Answers

1
votes

In writer:

for(int i=0;i<length;i++){ dataoutputstream.writeByte(bytearray[i]); }

In reader:

for(int i=0;i<length;i++){ bytearray[i]=datainputstream.readByte(); }