vendredi 20 février 2015

Send string array from java client to NodeJs server

I'm developing an Android app that will send a string array to an external NodeJs server running on the Raspberry Pi.


I'm currently handling the sending side of the app using an AsyncTask, with a socket layer setup inside targeted at the NodeJs server. The server is successfully receiving calls from the app using net and socket modules, however I'm struggling to pass actual data.


At the moment I'm using OutputStream to send data.



OutputStream socketStream = socket.getOutputStream();
ObjectOutputStream objectOutput = new ObjectOutputStream(socketStream);
objectOutput.writeObject(new String[] {"Test", "Test2", "Test3"});


Log.i("Array Object", objectOutput.toString());

objectOutput.close();
socketStream.close();


Is this the correct method of writing a string array to send to a NodeJs server?


Also what would the code contain on the NodeJs server in order to read this written data once it has been received?


Update


The NodeJs server is based off this tutorial http://ift.tt/1w5b7Lw


Where the function callback_server_connection is called in the net.createServer(callback_server_connection); function.



function callback_server_connection(socket){
var remoteAddress = socket.remoteAddress;
var remotePort = socket.remotePort;
socket.setNoDelay(true);
console.log("connected: ", remoteAddress, " : ", remotePort);

var msg = 'Hello ' + remoteAddress + ' : ' + remotePort + '\r\n'
+ "You are #" + count + '\r\n';
count++;

socket.end(msg);

socket.on('data', function (data) {
console.log(data.toString());
});

socket.on('end', function () {
console.log("ended: ", remoteAddress, " : ", remotePort);
});
}

Aucun commentaire:

Enregistrer un commentaire