title: Send Image from Java TCP Client to Node.js TCP Server
body: <p>I am trying to send an image from java TCP Client to Node.js TCP Server i have tried to: send binary data, send base64 encoded binary data,send utf-8 encoded strings and at the moment nothing has worked, i always got the data and it creates the image(using fs), but its always corrupted.
  Here is my code:</p>
  
  <p>JAVA CLIENT:</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  import java.net.Socket;
  
  public class client{
  
      public static void main(String[] args) throws Exception {
          Socket client = new Socket("localhost",8080);
          Scanner scanner = new Scanner(System.in);
          String path= scanner.nextLine();
          FileInputStream fis = new FileInputStream(path);
          byte[] buffer = new byte[fis.available()];
          fis.read(buffer);
          buffer= Base64.getEncoder().encode(buffer);
          System.out.println(buffer.length);
          ObjectOutputStream oos= new ObjectOutputStream(client.getOutputStream());
          oos.writeObject(buffer);
          client.close();
      }
  }
  </code></pre>
  
  <p>Node.js Server:</p>
  
  <pre><code>var fs = require('fs');
  var net = require('net');
  var sockets = [];
  var server=net.createServer().listen(8080,"localhost");
  
  server.on('connection', function(socket){
      console.log('Someone connected.');
      sockets.push(socket);
      var imageData;
  
      socket.on('data',function(data){
          imageData+= Buffer.from(data, 'base64');
      });
  
      socket.on('end',function(){
          console.log(imageData.length)
          fs.writeFileSync(new Date()+".png", imageData, function (err) {
            if (err) throw err;
            else  console.log('Saved!');
          });
      });
  
      socket.on('close',function(){
          console.log("Someone disconnected.");
      });
  });
  </code></pre>
  
  <p>please i need help, its the fourth time i made this question and nobody answers me, or they say something that doesn't help and give me -1.</p>
  
  <p><a href="https://i.stack.imgur.com/vRicP.png" rel="nofollow noreferrer">Here is the image output</a></p>
  
  <p><strong>UPDATE:
  <br>I tried working with strings and i first detected that the data length when received is not equal of the data length sent, so i removed the data in excess and still didn't work, i am thinking that fs library is bugged, can someone suggest me a better library that works with file in Node.js?</strong></p>