Broad Network


Node.js Socket Coding Basics

Internet Socket and ECMAScript - Part 3

Writing ECMAScript Module

Foreword: In this part of the series, I give you coding examples of a client and a server socket, for ECMAScript.

By: Chrysanthus Date Published: 24 Jul 2016

Introduction

This is part 3 of my series, Internet Socket and ECMAScript. In this part of the series, I give you coding examples of a client and a server socket, for ECMAScript. You should have read the previous parts of the series before coming here, as this is a continuation. In order to test the code samples you need to have Node.js installed in your computer.

Typically, in a client-server system, the server is put on first and it remains on. The client is put on next and it connects to the server. If the connection is successful, the server responds with a greeting message. After recieving the greeting, the client sends a request and recieves a reply, sends another request and receives another reply, sends a request and recieves a reply, and so on; until the client closes its socket and goes off. The server may stay on for another connection.

A Client Code
A client code that sends three specific lines of text to the server and receives a reply for each line is as follows (read through the code):

const net = require('net');

    const socket = net.createConnection(55555, 'localhost');

    socket.on('data', (data) =>
        {
            console.log(data.toString());
            receivedStr = data.toString();

            if (receivedStr.search(/^\s*Greeting/i) != -1)
                {
                        socket.write('the first line');
                }

            if (receivedStr.search(/^\s*first line received/i) != -1)
                {
                        socket.write('the second line');
                }

            if (receivedStr.search(/^\s*second line received/i) != -1)
                {
                        socket.write('the third line');
                }
            if (receivedStr.search(/^\s*third line received/i) != -1)
                {
                        socket.write('QUIT');
                        socket.end();
                }

        });

        socket.on('error', (err) =>
            {
                console.log(err);
                socket.end();
            });

A Server Code
A server code that receives three specific lines of text from the client and sends three corresponding replies, is as follows (read through the code):

const net = require('net');

    const server = net.createServer((clientSocket) => {
            // 'connection' listener

            clientSocket.write('Greeting');

            clientSocket.on('data', (data) =>
                {

                    console.log(data.toString());

                    receivedStr = data.toString();

                    if (receivedStr.search(/^\s*the first line/i) != -1)
                        {
                                clientSocket.write('first line received');
                        }
                    else if (receivedStr.search(/^\s*the second line/i) != -1)
                        {
                                clientSocket.write('second line received');
                        }
                    else if (receivedStr.search(/^\s*the third line/i) != -1)
                        {
                                clientSocket.write('third line received');
                        }
                    else
                        {
                            clientSocket.end();
                            server.close();
                            console.log('server closed');
                        }

                });

        });

    server.listen(55555, '127.0.0.1', 5);

    server.on('error', (err) =>
        {
            console.log(err);
            server.close();
        });

Testing the code Samples
Both code samples use localhost and port 55555. Assume that the name of the client file is, clientsock.js and the name of the server file is, serversock.js and both are in the root directory. At the drive c:\ prompt, you will type,

    node serversock.js

and press Enter, to run the server. And at the drive c:\ prompt in another DOS window, you will type,

    node clientsock.js

and press Enter, to run the client.

The output for the client is:

    Greeting
    first line received
    second line received
    third line received

and the output for the server is:

    the first line
    the second line
    the third line
    QUIT
    server closed

That is it for this part of the series.

Chrys

Related Links

Internet Socket and ECMAScript
Handling Bytes in ECMAScript
ECMAScript Bitwise Operators
ECMAScript Module Essentials
More Related Links
Node Mailsend
EMySQL API
Node.js Web Development Course
Major in Website Design
Low Level Programming - Writing ECMAScript Module
ECMAScript Course

BACK

Comments

Become the Writer's Follower
Send the Writer a Message