Heya!
I had a (hopefully) simple question regarding the behavior of close
on a TCP stream. We have some server which is taking inputs from a client and the spec requires us drop a connection if the client sends some malformed input. For some reason, there is a difference in the Node client based on two simple implementations of a server. Here is an example of this server in Julia:
errormonitor(@async begin
server = listen(IPv4(0), 8080)
@info "server started"
while true
sock = accept(server)
@info "received: $(readline(sock))"
close(sock)
end
end)
and here’s a client in NodeJS
const Net = require('net');
const client = new Net.Socket();
client.connect({ port: 8080, host: 'localhost' }, function() {
client.write('hello');
});
client.on('end', function() {
console.log('TCP connection dropped');
});
(this can be run, if you have nodejs on your system, by placing it into a file, say testclient.js
and running node testclient.js
.)
I would expect, running this client, that the result would be that the client prints “TCP connection dropped” to the console after writing to the server, but it seems that this is not the case. (Here, the connection is never dropped and the client happily continues on the socket.)
On the other hand, a similar implementation of the server in NodeJS (instead of Julia):
const Net = require('net');
const server = new Net.Server();
server.listen(8080, function() {
console.log(`server started`);
});
server.on('connection', function(socket) {
socket.on('data', function(chunk) {
console.log(`received: ${chunk.toString()}`);
socket.end();
});
});
does give the desired behavior.
Why does socket.end()
in NodeJS have such different behavior from close(socket)
in Julia? Additionally, is there any way to emulate the behavior of socket.end()
in Julia? (It seems to me, after some quick googling, that this might have to do with the fact that a FIN packet might not be sent to the client, but I am honestly not sure!)
Thank you so much!