PeelTechnologies / react-native-tcp

node's net api in react-native
MIT License
284 stars 212 forks source link

Cannot send data larger than about 0.5 MB on Android #79

Closed x3388638 closed 3 years ago

x3388638 commented 6 years ago

On Android device I can't send data larger than a fixed size, and the fixed size for each Android device is different.

The case is that I try to send a base64 string of a photo on an ASUS Zenfone 2 (ASUS_Z00AD) with Android 5.0, and the total size of data is 1489556 byte, but it sends only 655350 byte. For Samsung GALAXY Tab S (SM-T700) with Android 5.0.2, it always sends 1015808 byte if the data is larger than that. For Sony Xperia ZR (C5502) with Android 5.0.2, the fixed size is 507904 byte. However, it seems that there isn't a such limitation on iOS device (iPhone 6 Plus with iOS 11.3.1).

Is there any configuration limits the max size of data to send or any solution? Thanks!

The way to send and receive data:

const { Readable } = require('stream');
const socket = net.connect(PORT, IP, () => {
    const stream = new Readable();
    stream.push(dataBuffer); // buffer of image base64
    stream.push(null);
    stream.pipe(socket);
});
const server = net.createServer((socket) => {
    const chunks = [];

    socket.on('close', () => {
        const dataBuffer = Buffer.concat(chunks);
        // parse the dataBuffer
    });

    socket.on('data', (data) => {
        chunks.push(data);
    });
});
x3388638 commented 6 years ago

I find a dirty solution but I don't know why.

send data this way (300000 is just a number I set)

const stream = new Readable();
for (let i = 0; i < Math.ceil(dataBuffer.byteLength / 300000); i ++) {
    stream.push(dataBuffer.slice(i * 300000, i * 300000 + 300000));
}

stream.push(null);
stream.pipe(socket);
Overtorment commented 3 years ago

proper soluditon would be to start using react-native-tcp-socket instead of abandoned in 2017 package