Simple TCP/IP Client – Server Applications using NodeJS

TCP/IP (Transmission Control Protocol over Internet Protocol) is a most basic client – server communication topology between two network device in network. It has two basic protocols: TCP and IP.

Topology of Client – Server communication based on TCP/IP

Implement in NodeJS

1. Server

a. Call modules

// Asynchronous network API module
var net = require('net');
// File system module to store data
var fs = require('fs');

b. Create server

// Create Server
var server = net.createServer(function(socket) {
    // Log file
    var logfile = fs.createWriteStream('server.log',{flags: 'a'});

    // Callback function when there is error
    logfile.on('error', function(err) {
        // Print error to terminal
        console.log("Log file: ERROR: " + err);
    });

    // Forward received data to log file
    socket.pipe(logfile);

    // Callback function when data arrives
    socket.on('data', function(data){
        // Print raw data to terminal
        console.log(data);
        // Format data to UTF8 characters
        var textChunk = data.toString('utf8');
        // Print formatted data to terminal
        console.log(textChunk);
        // Respond to client
        socket.write("OK\r\n");
    });

    // Callback function when client disconnected
    socket.on('end', function() {
        console.log("Server: Client disconnected");        
    });

    // Callback function when there is error
    socket.on('error', function(err) {
        // Print error to terminal
        console.log("Server: ERROR: " + err);
    });
});

c. Configure port

// Open port 2021
server.listen(2021, function() {
    // Print to terminal if success
    console.log("Server: listening !\r\n");
});

2. Client

a. Call modules

// Asynchronous network API module
var net = require('net');
// File system module to store data
var fs = require('fs');

b. Create client

// Create Client
var client = new net.Socket();

c. Connect to server

// Connect to server
client.connect(2021, 'localhost', function() {
    // Print to terminal if success
    console.log('Connected');

    // Create stream for log file    
    var logfile = fs.createWriteStream('client.log',{flags: 'a'});
    // Callback function when there is error
    logfile.on('error', function(err) {
        // Print error to terminal
        console.log("Log file: ERROR: " + err);
    });

    // Forward received data to log file
    client.pipe(logfile);

    // Send to Server
    client.write("Sent from client!");
});

d. Receive response from server

// Callback function when data arrives
client.on('data', function(data) {
    // Print to terminal
    console.log('Received: ' + data);
    // Kill client after server's response
    client.destroy();
});

e. Disconnection event

// Callback function when client disconnected
client.on('close', function() {
    // Print to terminal
    console.log('Connection closed');
});

3. Test

Run server on terminal:

nodejs tcp_server.js

Run client on termial:

nodejs tcp_client.js

4. Reference

https://github.com/bigdolphin/tcp-ip-nodejs

https://nodejs.org/api/net.html

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *