You use the net.Server
object to create a TCP socket server and begin listening for connections to which you will be able to read and write data. The Server
object is created internally when you call net.createServer()
. This object is intended to represent the socket server and handles listening for connections and then sending and receiving data on those connections to the server.
When the server receives a connection, the server creates a Socket
object and passes it to any connection event handlers that are listening. Because the Socket
object implements a Duplex
stream, you can use it with the write()
method to stream writes of data back to the client and a data
event handler to stream data from the client.
To create a Server
object, you use the net.createServer()
method:
net.createServer([options], [connectionListener])
The options
parameter is an object that specifies options to use when creating the socket Server
object. Table 8.5 lists the option of the Server
object. The second parameter is the connection
event callback function, which is executed when a connection is received. This connectionListenter
callback function is passed the Socket
object for the connecting client.
Once the Server
object is created, it provides several events that are triggered during the life cycle of the server. For example, the connection
event is triggered when a socket client connects, and the close
event is triggered when the server shuts down. As you implement your socket server, you can register callbacks to be executed when these events are triggered to handle connections, errors, and shutdown. Table 8.6 lists the events that can be triggered on Socket
objects.
The Server
object also includes several methods that allow you to do things like read from and write to the socket as well as pause or end data flow. Many of these are inherited from the Duplex
stream objects, so they should be familiar to you. Table 8.7 lists the methods available on Socket
objects.
The Server
object also provides the maxConnections
attribute, which allows you to set the maximum number of connections that the server accepts before rejecting them. If a process has been forked to a child for processing using child_process.fork()
, you should not use this option.
The following code shows the basics of implementing the Server
object:
var net = require('net'),
var server = net.createServer(function(client) {
console.log('Client connected'),
client.on('data', function(data) {
console.log('Client sent ' + data.toString());
});
client.on('end', function() {
console.log('Client disconnected'),
});
client.write('Hello'),
});
server.listen(8107, function() {
console.log('Server listening for connections'),
});
Notice that the net.createServer()
method implements a callback that accepts the client Socket
object. To handle data coming back from the client, the on.data()
event handler is implemented. To handle the closure of the socket, the on('end')
event handler is implemented. To begin listening for connections, the listen()
method is called on port 8107
.