How to connect to a telnet server from node using

2020-07-23 04:35发布

问题:

I may not be entering the correct search terms but I cannot seem to find good examples that allow my node application to initiate a socket.io client connection to another telnet server (non-node).

Below is my node app trying to connect to a telnet server

var ioc = require('socket.io-client'),
clientSocket = ioc.connect('192.168.1.97', {
    port: 23
});
clientSocket.on('connect', function(){
  console.log('connected to to telnet');
});
clientSocket.on('connect_error', function(data){
  console.log('connection error to telnet');
  console.log(data);
});
clientSocket.on('connect_timeout', function(data){
  console.log('connection timeout to telnet');
  console.log(data);
});

Here is the error I get

connection error to telnet
timeout
connection timeout to telnet
20000

I've telneted directly to the telnet server successfully from the terminal. Bad code?

回答1:

You can't.

Socket.IO has nothing to do with regular TCP network sockets. Socket.IO is an RPC layer providing web-socket-like functionality over several transports (Web Sockets, long-polling AJAX, etc.). You can't just connect to any server you want, you must connect to a Socket.IO server. Even Web Sockets itself has a whole protocol built on top of HTTP that must be set up.

If you want to connect to an arbitrary server to send/receive data, that connection must be proxied server-side through your Node.js application. Socket.IO is only for communication between a Socket.IO client and a Socket.IO server.



回答2:

Not Sure if this can be done, but have a look at this package

https://www.npmjs.org/package/node-telnet-client