0

有人可以告诉我如何将 linemode 设置为 telnet 测试吗?我google了很多,但不幸的是我没有成功。我有这个超级简单的代码

var net = require('net');

var server = net.createServer(function(c) {
  console.log('socket opened');
  c.setEncoding('utf8');
  c.on('end', function() {
    console.log('connection/socket closed');
  });
  c.on('data', function(data) {
    console.log('Data:'+data);
    c.write('Answer:'+data);
    // c.end(); // close socket/conection
  });
});

server.listen(8124, function() { // start server (port 8124)
  console.log('server started');
});

但测试看起来像这样:

客户:

Microsoft Telnet> open localhost 8124
Answer:dAnswer:daAnswer:atAnswer:taAnswer:a

服务器:

C:\Users\Laura\node server.js
server started
socket opened
Data:d
Data:a
Data:t
Data:a
4

1 回答 1

0

你想得到如下的telnet输入

Answer: a
Answer: b
Answer: c

那么你需要\r is carriage returnline feed (\n). 所以你的代码应该像下面这样

var server = net.createServer(function(c) {
    console.log('socket opened');
    c.setEncoding('utf8');
    c.on('end', function() {
        console.log('connection/socket closed');
    });
    c.on('data', function(data) {
        console.log('Data:'+data);
        c.write('\r\nAnswer:');
        // c.end(); // close socket/conection
    });
});
于 2013-03-30T09:18:41.217 回答