I am trying to make a simple Node.js script which creates a server if the assigned port is not used, or connects to the server if the port is being used. I am using a recursive approach to this (using this link as reference), so I am running the same .js file on two different cmd windows.
The problem is that whenever the client connects to the server, it automatically goes into the 'end' listener, and I don't know why. After reading the docs, I found out about the allowHalfOpen property, of which the following is said in the 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.
I thought this was the obvious solution, so I set the property to true on the server, but that didn't work either.
Here is the code:
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");
And here are the outputs from the console:
For the Server:
For the Client:
What I want essentially is to have the client disconnect only when the server closes or when some other event occurs. But ideally I want it to be connected to the server for exchange of data between them.
Is there something I am doing wrong? Why is the FIN package still sent even when allowHalfOpen is true on the Server?
Thanks,
I saw my mistake, I had the following line:
socket.end("hello");
Inside the server code, which essentially killed the client anyway.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With