D
D
Dark Hole2016-04-26 18:19:02
Node.js
Dark Hole, 2016-04-26 18:19:02

Telnet + Node.js (io.js). How to make control via telnet possible?

Speaking essentially, I wanted to have a local server on my computer so that I could manage it (my whim)))).
Why do you need to resort to telnet?
- Just a server, I can do it myself
- Everyone knows how to use a browser, but only a few know about telnet
- You need to be an Indian)
So, is it possible to do this at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2016-04-27
@bingo347

For someone on the toaster already wrote this example:

'use strict';

const net = require('net');

const server = net.createServer(socket => {
    var data = '';
    socket.on('data', d => {
        data += d;
        var p = data.indexOf('\n');
        if(~p) {
            let cmd = data.substr(0, p);
            data = data.slice(p + 1);
            onCommand(cmd.trim(), socket);
        }
    });
});

server.listen(() => {
    var address = server.address();
    console.log('opened server on', address);
});

function onCommand(cmd, socket) {
    switch(cmd) {
        case 'open':
            socket.write('opened\n');
            break;
        case 'add':
            socket.write('added\n');
            break;
        case 'process':
            socket.write('processed\n');
            break;
    }
}

tested command input through telnet, if desired, you can modify

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question