Node has an out-of-the-box TCP server implementation. This can be accessed through the 'net' module.
Creating a TCP server:
var net = require('net');
var server = net.createServer(function(socket) {
//code to handle each connection
});
This is fairly similar to HTTP server in Node. The createServer creates a Server object and takes in a function callback that will be called to handle each connection. This function will be supplied the Socket object of the incoming connection. It is a two-way stream object, meaning you can send as well as receive data with it.
net.Server is an EventEmitter, which means that it emits certain events. They are:
listening - when it starts listening for connections
connection - when a connection is received. Handling this event is same as passing a function callback to createServer()
close - when a server is closed and not bound to any port
error - when an error occurs at a server level
Socket is a Stream
As the Socket object is a bi-directional Stream, it can be read from or written to. It emits data and end events whenever receiving data. And you can write data to it using write() function. You can also tell the server to close the connection by calling end().
You can also performing piping operations involving the Socket stream. The following code, for example, sends the contents of a text file to the Socket by piping:
var net = require('net');
var fs = require('fs');
var server = net.createServer(function(socket){
var fileStream = fs.createReadStream('test.txt');
fileStream.pipe(socket, {end: false});
});
server.on('error', function(e){
console.log("Server error: " + e.message);
});
server.listen(3001);
Creating a TCP server:
var net = require('net');
var server = net.createServer(function(socket) {
//code to handle each connection
});
This is fairly similar to HTTP server in Node. The createServer creates a Server object and takes in a function callback that will be called to handle each connection. This function will be supplied the Socket object of the incoming connection. It is a two-way stream object, meaning you can send as well as receive data with it.
net.Server is an EventEmitter, which means that it emits certain events. They are:
listening - when it starts listening for connections
connection - when a connection is received. Handling this event is same as passing a function callback to createServer()
close - when a server is closed and not bound to any port
error - when an error occurs at a server level
Socket is a Stream
As the Socket object is a bi-directional Stream, it can be read from or written to. It emits data and end events whenever receiving data. And you can write data to it using write() function. You can also tell the server to close the connection by calling end().
You can also performing piping operations involving the Socket stream. The following code, for example, sends the contents of a text file to the Socket by piping:
var net = require('net');
var fs = require('fs');
var server = net.createServer(function(socket){
var fileStream = fs.createReadStream('test.txt');
fileStream.pipe(socket, {end: false});
});
server.on('error', function(e){
console.log("Server error: " + e.message);
});
server.listen(3001);