Search code examples
node.jstelnet

telnet using node js


1.I have tried using node js to connect telnet. I am trying to control the Epson projector using node js and telnet from my computer.

2.I am running the js code in cmd by: "node filelocation\test.js"

The code i used:

const telnet = require('telnet-client');
const server = new telnet();

// display server response
server.on("data", function(data){
    console.log(''+data);
});

// login when connected
server.on("connect", function(){
    server.write("%1POWR 1\r\n");
});

// connect to server
server.connect({
    host: "192.168.2.170",
    port: 4352
});

I am running the js code in cmd by: "node filelocation\test.js"

The error i get is :

TypeError: server.write is not a function
    at Telnet.<anonymous> (C:\Users\USER\Desktop\TEST\telnet2.js:11:12)
    at Telnet.emit (events.js:314:20)
    at Socket.<anonymous> (C:\Users\USER\node_modules\telnet-client\lib\index.js:70:16)
    at Object.onceWrapper (events.js:420:28)
    at Socket.emit (events.js:326:22)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1132:10)

Solution

  • I used this code and it worked for me. Thanks all for the support.

     var net = require('net');
        
        var client = new net.Socket();
        client.connect(4352, 'x.x.x.x', function() {
            console.log('Connected');
            client.write('%1POWR 0\r\n');
        });
        
        client.on('data', function(data) {
            console.log('Received: ' + data);
            client.destroy(); // kill client after server's response
        });
        
        client.on('close', function() {
            console.log('Connection closed');
        });