jacobbb
jacobbb

Reputation: 101

send a big byte array through java socket

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.

Upvotes: 0

Views: 1426

Answers (1)

Andrik007
Andrik007

Reputation: 57

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(); }

Upvotes: 1

Related Questions