The net module provides an asynchronous networking API for creating stream-based TCP (transmission control protocol) or IPC (inter-process communication) servers. One of the most common transport layer protocols is TCP. TCP is connection-oriented.
Run the code below via node server.js
server.js
1. const net = require("net");
2.
3. const server = net.createServer();
4. const port = 32753;
5.
6: server.on('connection', function (s) {
7: console.log(`Client connected: ${s.remoteAddress} ${s.remotePort}`);
8: s.write(`hello world from server`);
9: s.on('data', function (data) {
10: console.log(`server: message received ${data.toString()}`);
11: });
12: s.on('close', function (err) {
13: if (err) {
14: console.log(`error: client disconnected`);
15: } else {
16: console.log(`client disconnected`);
17: }
18: });
19: });
20:
21: server.on('listening', function () {
22: console.log(`Server is listening on port: ${port}`);
23: });
24:
25: server.listen(port);
Server Side Explanation:
The net module is imported on line 1.
The createServer method invoked on line 3, creates a server object. This server is an object of the EventEmitter class; hence, so we use the on method to handle different events.
The port is set to 32753 on line 4.
The connection event fires when a new connection to the server is made. On line 6, this event returns an object that we name s. This socket object is an instance of Socket.
The Socket also belongs to the EventEmitter class.
The remoteAddress and remotePort are two attributes of the Socket object that we use to output the IP address and port of the incoming connection on line 7.
Data is sent on the socket by using the write method. This is done on line 8.
The data event is fired when data from a client is received. The on event is defined on line 9. We are outputting the data on line 10.
The close event (captured on line 12) fires when a socket is closed, and indicates that communication has ended. If the socket closes due to an error, the hadError boolean is set to true. We are outputting both cases on line 14 and line 16.
For the server object, the listening event (captured on line 21) fires when the server starts to listen for incoming connections. We use this event to output a string to let us know that the server is up, on line 22.
Lastly, we start the server, using the listen method, on line 25. Now our server is live and waiting for new connections.
Run the code below via node client.js
client.js
1. const net = require('net');
2.
3. const client = net.connect({ port: 32753 }, () => {
3. console.log(`client connected`);
4. client.write(`hello world from client`);
5. });
6.
7. client.on('data', (data) => {
8. console.log(`message received: ${data.toString()}`, );
9. });
10.
11. client.on('end', () => {
12. console.log(`client disconnected`);
13. });
Client Side Explanation:
Looking from the client side, another useful method is the connect() method. We pass it the port number that our server is listening on, 32753. This method also returns a Socket object like our connection event did on the server side. This is done on line 3.
The data (line 7) and end (line 11) methods are used in a similar to the server side.
Output from client:
client connected
message received: hello world from server
Output from server:
Server is listening on port: 32753
Client connected: ::ffff:127.0.0.1 64003
server: message received hello world from client
Reference: