I am looking to convert a java thread-based tcp socket server to javascript-based (preferably nodejs) async tcp socket server. It's my app and now learning to nodejs. Thought it would be a good exercise to learn more about nodejs this way.
I've looked at nodejs and would be using the net module. I've also read that nodejs is event based and does not do thread-based stuff.
Any ideas?
The question is actually pretty vague, but I'll try to answer posting a link of a very simple TCP server I've seen done in nodeJS:
http://www.hacksparrow.com/tcp-socket-programming-in-node-js.html
You're right, you have to set up a few events upon that you will have to do your data processing and so on, but it pretty much depends on what your server is for, what kind of data it will receive and what it is expected to produce.
If you give us some more details on your question, I guess we'll be able to better clarify its functioning.
ok perfect, so following to your new details, you'll have to do something like this to start listening:
var server = net.createServer();
server.listen(PORT, HOST);
console.log('Server listening on ' + server.address().address +':'+server.address().port);
and then this to listen for the connection and to process the data:
server.on('connection', function(sock) {
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ': ' + data);
// here you'll do all your data processing, for instance
// call some JSON webservices that will write SQL queries...
});
});