Monday 1 May 2017

How to create a tcp client that connects to a server's CLI using NodeJS?

I am working on a project and I have watched tutorials, looked at many of my resources and feel that I just need to put the scope of my project on here and get as much insights and help as I can.

Here it is:

  • Requirements:

    • Create a tcp client that connects to one of our server's CLI.
    • You may use any language (though I am particularly interested in a JavaScript solution)
    • This client must be able to interactively accept input and send that input to the server to be processed and then display the server's output. (You'll probably need a while loop to be able to continuously accept input...)

    Things To Know:

    • The server can be reached at 107.23.62.11:6200
    • The message structure is in json like so: {"Type":115, "Args":{"Sender":"CLI"}}
    • The tag must always come directly after the json message
    • The type 115 is used as the login command to the server (see the above example). After successful login, subsequent commands should use type 168 (this type is used to indicate to the server that the message is to be processed by the cli). e.g. {"Type":168, "Args":{}}
    • After successful login the "Args" object in the message json should include two entries:
      1. The command that was read from the input stream (specified by the "Command" key)
      2. The hash of the command and the string "job_interview" appended to the command. (The hash must be created using the MD5 algorithm, and specified in json by the "Hash" key) Here is an example: If the command was: help, the string to be hashed would be: helpjob_interview Here is the json: {"Type":168, "Args":{"Command":"help", "Hash":"E698E4D6D5B2029C124D2451E143C96D"}}
    • The response from the server will be in the format: {"Type":168, "Args":{"Response":""}}

    Notes:

    • All messages to the server must be followed by:
    • All messages from the server will be followed by:
    • The login command must be sent within 5 seconds of connecting to the server... Otherwise you will be disconnected.
    • You will be disconnected after one minute of inactivity, so please send a heartbeat at regular intervals with the command: {"Type":999} (Don't forget the !)
    • Before the response from the server can be displayed please parse it to be human readable. Not human readable: {"Type":168, "Args":{"Response":"This is a response."}} Is human readable: This is a response.
    • Please feel free to use any language you feel comfortable using.

This is what I have so far:

//include module
var net = require('net');
//create server
var server = net.Server();
//make it listen
server.listen(6200, '107.23.62.11'); //107.23.62.11

server.on('connection', function (socket) {
    socket.write('Hi client!');

    socket.on('data', function (data) {
        console.log('Data from client: ' + data);
    });
});

setTimeout(function () {
    //create client
    var connection = net.connect(6200, '107.23.62.11', function () {
        connection.write('Hi there server!');

        setInterval(function () {
            connection.write('ping');
        }, 1000 * 2);
        connection.on('data', function (data) {
            console.log('Data from the server: ' + data);
        });
    });
}, 1000 * 2);



via Saia Fonua

No comments:

Post a Comment