Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 简单的Telnet服务器和客户端连接不起作用_Node.js_Sockets - Fatal编程技术网

Node.js 简单的Telnet服务器和客户端连接不起作用

Node.js 简单的Telnet服务器和客户端连接不起作用,node.js,sockets,Node.js,Sockets,我制作了一个简单的Node.js服务器,用于侦听TCP套接字连接 // Include Nodejs' net module. const Net = require('net'); // The port on which the server is listening. const port = 23; // Create a new TCP server. const server = new Net.Server(); // The server listens to a socket

我制作了一个简单的Node.js服务器,用于侦听TCP套接字连接

// Include Nodejs' net module.
const Net = require('net');
// The port on which the server is listening.
const port = 23;

// Create a new TCP server.
const server = new Net.Server();
// The server listens to a socket for a client to make a connection request.
server.listen(port, function() {
    console.log('Server listening for connection requests on socket localhost:' + port);
});

// When a client requests a connection to the server, the server creates a new
// socket dedicated to that client.
server.on('connection', function(socket) {
    
    console.log('A new connection has been established.');

    console.log('---------server details -----------------');

    var address = server.address();
    var port = address.port;
    var family = address.family;
    var ipaddr = address.address;
    console.log('Server is listening at port' + port);
    console.log('Server ip :' + ipaddr);
    console.log('Server is IP4/IP6 : ' + family);

    var lport = socket.localPort;
    var laddr = socket.localAddress;
    console.log('Server is listening at LOCAL port' + lport);
    console.log('Server LOCAL ip :' + laddr);

    console.log('------------remote client info --------------');

    var rport = socket.remotePort;
    var raddr = socket.remoteAddress;
    var rfamily = socket.remoteFamily;

    console.log('REMOTE Socket is listening at port' + rport);
    console.log('REMOTE Socket ip :' + raddr);
    console.log('REMOTE Socket is IP4/IP6 : ' + rfamily);

    console.log('--------------------------------------------')
    // Now that a TCP connection has been established, the server can send data to
    // the client by writing to its socket.
    socket.write('Hello, client.\r\n');

    // The server can also receive data from the client by reading from its socket.
    socket.on('data', function(chunk) {
        console.log('Data received from client: ' + chunk.toString());
        socket.write('success\r\n', function() {
            console.log('Successfully Write to Client');
        });
    });

    // When the client requests to end the TCP connection with the server, the server
    // ends the connection.
    socket.on('end', function() {
        console.log('Closing connection with the client');
    });

    // Don't forget to catch error, for your own sake.
    socket.on('error', function(err) {
        console.log(`Error: ${err}`);
    });
});
客户端代码是

Telnet = require 'telnet-client'
telnet = new Telnet()
...
telnet.on 'connect', () ->
            if timeout
                clearTimeout timeout
            logger.log 'TELNET: connected'
            telnet.exec "login #{config.username} #{config.password}"

telnet.on 'data', (data) -> 
            logger.log data

telnet.connect { host: config.host, port: config.port, shellPrompt: config.shellPrompt, timeout: config.timeout }
客户端代码是coffescript。你不必担心承诺方法

如您所见,我需要在建立连接后从服务器获取数据。在服务器中,socket.write(数据)运行,但在客户端代码中,我无法使用telnet.on“data”获取数据。。。 有什么问题