简体   繁体   English

通过Java套接字发送大字节数组

[英]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. 我有一个长度超过1200000的大字节数组。我想通过DataOutputStream发送它,并通过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. out是DataOutputStream,in是DataInputStream,outData是我要发送的字节数组。 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. 当我运行程序时,如果字节数组的长度在120000左右,则可以发送该数组,但是当长度变为1200000时,服务器将无法接收该数组。 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. 谢谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM