Node.js:客户端与服务器断开连接,即使 allowHalfOpen 设置为 True
Node.js: Client Disconnects from Server even with allowHalfOpen set to True
我正在尝试制作一个简单的 Node.js 脚本,如果分配的端口未被使用,它会创建一个服务器,或者如果该端口正在被使用,则会连接到服务器。我正在对此使用递归方法(使用 this link 作为参考),所以我 运行 在两个不同的 cmd windows.[=16 上使用相同的 .js 文件=]
问题是每当客户端连接到服务器时,它会自动进入'end'监听器,我不知道为什么。看完文档,我发现了allowHalfOpen属性,其中在API:
中说了以下内容
If allowHalfOpen is true, then the socket won't automatically send a FIN packet when the other end of the socket sends a FIN packet.
我认为这是显而易见的解决方案,所以我在服务器上将 属性 设置为 true,但这也不起作用。
代码如下:
var net = require('net');
var PORT_NUMBER = 1337;
connectToPort = function(connection) {
if(connection === "server") { // Connect as server to the port.
connectAsServer();
} else if (connection == "client") { // Connect as client to the port.
connectAsClient();
} else {
console.log("Incorrect Type Of Connection Passed.");
}
}
connectAsServer = function() {
// Attempt to create server.
var server = net.createServer({allowHalfOpen:true}, function (socket) {
socket.allowHalfOpen = true;
socket.write('Hello from Server\r\n');
socket.end("hello");
console.log("someone went into server.");
socket.pipe(socket);
});
console.log("Trying to Open Port " + PORT_NUMBER);
server.listen(PORT_NUMBER, function() {
console.log("\nServer 2 is now the official server. \n");
});
server.on('error', function(err) {
console.log("error is " + err.code);
if(err.code == 'EADDRINUSE') {
console.log('Port is in use, becoming client...');
setTimeout(function () {
server.close();
connectToPort("client");
}, 1000);
}
if(err.code == 'ECONNRESET') {
console.log("connection reset error.");
}
});
server.on('end', function() {
console.log("server 2 disconnected from port");
});
}
connectAsClient = function() {
var client = net.connect({port: PORT_NUMBER}, function() { //'connect' listener
console.log('THIS IS CLIENT connected to server!');
});
client.on('data', function(data) {
console.log(data.toString());
client.write('hello world! from client!!\r\n');
});
client.on('end', function() {
// client automatically goes here.
console.log('disconnected from server');
});
client.on('error', function(err) {
console.log("there was an error in the client and the error is: " + err.code);
});
}
connectToPort("server");
控制台的输出如下:
对于服务器:
对于客户:
我想要的本质上是让客户端仅在服务器关闭或其他事件发生时断开连接。但理想情况下,我希望它连接到服务器以在它们之间交换数据。
我做错了什么吗?为什么服务端的allowHalfOpen为真,FIN包还是发送?
谢谢,
我看到了我的错误,我有以下行:
socket.end("hello");
在服务器代码内部,这基本上杀死了客户端。
我正在尝试制作一个简单的 Node.js 脚本,如果分配的端口未被使用,它会创建一个服务器,或者如果该端口正在被使用,则会连接到服务器。我正在对此使用递归方法(使用 this link 作为参考),所以我 运行 在两个不同的 cmd windows.[=16 上使用相同的 .js 文件=]
问题是每当客户端连接到服务器时,它会自动进入'end'监听器,我不知道为什么。看完文档,我发现了allowHalfOpen属性,其中在API:
中说了以下内容If allowHalfOpen is true, then the socket won't automatically send a FIN packet when the other end of the socket sends a FIN packet.
我认为这是显而易见的解决方案,所以我在服务器上将 属性 设置为 true,但这也不起作用。
代码如下:
var net = require('net');
var PORT_NUMBER = 1337;
connectToPort = function(connection) {
if(connection === "server") { // Connect as server to the port.
connectAsServer();
} else if (connection == "client") { // Connect as client to the port.
connectAsClient();
} else {
console.log("Incorrect Type Of Connection Passed.");
}
}
connectAsServer = function() {
// Attempt to create server.
var server = net.createServer({allowHalfOpen:true}, function (socket) {
socket.allowHalfOpen = true;
socket.write('Hello from Server\r\n');
socket.end("hello");
console.log("someone went into server.");
socket.pipe(socket);
});
console.log("Trying to Open Port " + PORT_NUMBER);
server.listen(PORT_NUMBER, function() {
console.log("\nServer 2 is now the official server. \n");
});
server.on('error', function(err) {
console.log("error is " + err.code);
if(err.code == 'EADDRINUSE') {
console.log('Port is in use, becoming client...');
setTimeout(function () {
server.close();
connectToPort("client");
}, 1000);
}
if(err.code == 'ECONNRESET') {
console.log("connection reset error.");
}
});
server.on('end', function() {
console.log("server 2 disconnected from port");
});
}
connectAsClient = function() {
var client = net.connect({port: PORT_NUMBER}, function() { //'connect' listener
console.log('THIS IS CLIENT connected to server!');
});
client.on('data', function(data) {
console.log(data.toString());
client.write('hello world! from client!!\r\n');
});
client.on('end', function() {
// client automatically goes here.
console.log('disconnected from server');
});
client.on('error', function(err) {
console.log("there was an error in the client and the error is: " + err.code);
});
}
connectToPort("server");
控制台的输出如下:
对于服务器:
对于客户:
我想要的本质上是让客户端仅在服务器关闭或其他事件发生时断开连接。但理想情况下,我希望它连接到服务器以在它们之间交换数据。
我做错了什么吗?为什么服务端的allowHalfOpen为真,FIN包还是发送?
谢谢,
我看到了我的错误,我有以下行:
socket.end("hello");
在服务器代码内部,这基本上杀死了客户端。