简体   繁体   中英

How to send image from [python client] to [node.js server] using tcp socket?

I want to send a captured image from the raspberry pie client (python) to the server (node.js). We encoded the image as base64 and sent it back to the server using decoding the base64 as an image, but the image was broken because the file was in a different format.

Here is my code:

client.py

import base64

from PIL import Image
import os, sys

ip = ''
port = 3008
s = socket.socket()
s.connect((ip, port))

image_path = '/home/pi/TCPproject/test.jpg'

if image_path != '':
    with open(image_path, "rb") as imageFile:
        image_data = base64.b64encode(imageFile.read())
else:
    image_data = 'cusdom_image'


s.send(image_data)

s.close()

server.js

var fs = require('fs');
var base64ToImage = require('base64-to-image');
var sockets = [];

var server = net_server.createServer(function(client) {
    console.log('Client connection: ');
    console.log('local = %s:%s', client.localAddress, client.localPort);
    console.log('remote = %s:%s', client.remoteAddress, client.remotePort);
    client.setTimeout(500);
    client.setEncoding('utf8');
    sockets.push(client);
    var imageData;

    client.on('data', function(data) {
        imageData+= data;
    });

    client.on('end', function() {
        console.log('end!')
        var decoded = Buffer.from(imageData, 'base64');
        fs.writeFile("test.jpg", decoded, function (err) {
            if (err) throw err;
            else  console.log('Saved!');
        });
    });

    client.on('error', function(err) {
        console.log('Socket Error: ', JSON.stringify(err));
    });

    client.on('timeout', function() {
        console.log('Socket Timed out');
    });
});

server.listen(3008, function() {
    console.log('Server listening: ' + JSON.stringify(server.address()));

    server.on('close', function(){
        console.log('Server Terminated');
    });

    server.on('error', function(err){
        console.log('Server Error: ', JSON.stringify(err));
    });
});

function writeData(socket, data){
    var success = socket.write(data);
    if (!success){
        console.log("Client Send Fail");
    }
}

Please let me know if encoding, decoding is wrong, or if the TCP socket communication process is wrong, or if there is another problem.

There are multiple problems with the code. On the client side:

 s.send(image_data)

This might send image_data but it might only send part of image_data since send is not guaranteed to send everything. Use sendall to send everything or check the return value of send and make sure to send the rest later if not everything was sent at once.

On the server side:

var imageData;

client.on('data', function(data) {
    imageData+= data;
});

client.on('end', function() {
    console.log('end!')
    var decoded = Buffer.from(imageData, 'base64');

If you would have a look at imageData before decoding you would see that it starts with the string undefined and only then the base64 data are following. But all of this is treated as input to the base64 decoder, leading to corrupt data. To fix this initialize imageData :

var imageData = '';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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