我正在尝试使用tcp工作来获取nodejs客户端服务器。这是我的代码:
包含服务器代码的server.js
var net = require('net');
var server = net.createServer(function(socket) {
socket.write('Echo server\r\n');
socket.pipe(socket);
});
console.log("Started Server");
server.listen(1337, '127.0.0.1');
包含客户端代码的client.js
var net = require('net');
var client = new net.Socket();
client.connect(1337, '127.0.0.1', function() {
console.log('Connected');
client.write('Hello, server! Love, Client.');
});
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');
});
当我在2个单独的终端节点server.js中运行时,节点client.js我收到此错误:
Started Server
events.js:154
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at exports._errnoException (util.js:856:11)
at TCP.onread (net.js:546:26)
Process finished with exit code 1
但如果我在一个过程中将两个代码组合在一起就可以正常工作。任何人都知道问题是什么?