Comunicar Node JS com java via socket

Gostaria de tirar uma dúvida em como comunicar 2 servidores, um sendo nodejs e outro sendo java.

Gostaria de transitar informações json entre os 2 utilizando o suporte de socket.

Consegui enviar uma informação do java pra o node e receber la decodificando o base64, mais para enviar outra mensagem do nodejs para o java receber não estou conseguindo… Alguem pode ajudar?

Código node:

var net = require(‘net’);

var HOST = ‘127.0.0.1’;
var PORT = 6969;

global.Buffer = global.Buffer || require(‘buffer’).Buffer;

function Utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3;

out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4)
{ 
  case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
    // 0xxxxxxx
    out += String.fromCharCode(c);
    break;
  case 12: case 13:
    // 110x xxxx   10xx xxxx
    char2 = array[i++];
    out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
    break;
  case 14:
    // 1110 xxxx  10xx xxxx  10xx xxxx
    char2 = array[i++];
    char3 = array[i++];
    out += String.fromCharCode(((c & 0x0F) << 12) |
                   ((char2 & 0x3F) << 6) |
                   ((char3 & 0x3F) << 0));
    break;
}
}
return out;

}

net.createServer(function(sock) {
console.log(‘CONNECTED: ’ + sock.remoteAddress +’:’+ sock.remotePort);

  sock.on('data', function(data) {
      console.log(new Buffer(data, 'base64').toString());
      console.log(new Buffer('Mensagem recebida com sucesso').toString('base64'));
      sock.emit('teste');
  });
  sock.on('close', function(data) {
      console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
  });

}).listen(PORT, HOST);

console.log(‘Server listening on ’ + HOST +’:’+ PORT);

E meu código rodando no client java:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**

  • Classe Java (client) para Comunicação com servidor NodeJS Escreve e recebe um
  • simples “echo” -> “mensagem123” by Douglas.Pasqua http://douglaspasqua.com
    */
    public class NodeJsEcho {
    // objeto socket
    private Socket socket = null;
    private static boolean close = false;

public static void main(String[] args) throws UnknownHostException, IOException, ClassNotFoundException {
// instancia classe
NodeJsEcho client = new NodeJsEcho();

  // conexão socket tcp
  String ip = "127.0.0.1";
  int port = 6969;
  client.socketConnect(ip, port);
  // escreve e recebe mensagem
  String message = "mensagem123";
  while(!close){
  	System.out.println("Enviando: " + message);
  	String retorno = client.echo(message);
  	System.out.println("Recebendo: " + retorno);
  	close = true;
  }

}

// realiza a conexão com o socket
private void socketConnect(String ip, int port) throws UnknownHostException, IOException {
System.out.println("[Conectando socket…]");
this.socket = new Socket(ip, port);
}

// escreve e recebe mensagem full no socket (String)
public String echo(String message) {
try {
// out & in
PrintWriter out = new PrintWriter(getSocket().getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(getSocket().getInputStream()));

  	// escreve str no socket e lêr
  	out.println(message);
  	String retorno = in.readLine();
  	return retorno;
  } catch (IOException e) {
  	e.printStackTrace();
  }
  return null;

}

// obtem instância do socket
private Socket getSocket() {
return socket;
}
}

Eu só consegui receber a informação do java no node, e ter acesso a ela, mais ao enviar uma do node para o java não consigo ter acesso, nem chega a receber a mensagem.

Obrigado.