# Closing a TCP stream (behavior difference from NodeJS?)

**URL:** <https://discourse.julialang.org/t/closing-a-tcp-stream-behavior-difference-from-nodejs/79140>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 7, 2022, 5:12am UTC](https://discourse.julialang.org/t/closing-a-tcp-stream-behavior-difference-from-nodejs/79140 "2022-04-07T05:12:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![guille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guille/32/7505_2.png) [@guille](https://discourse.julialang.org/u/guille)\
**Post date:** [April 7, 2022, 5:12am UTC](https://discourse.julialang.org/t/closing-a-tcp-stream-behavior-difference-from-nodejs/79140/1 "2022-04-07T05:12:51Z")

</div>

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:

```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

```javascript
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):

```javascript
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!

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [April 7, 2022, 7:46am UTC](https://discourse.julialang.org/t/closing-a-tcp-stream-behavior-difference-from-nodejs/79140/2 "2022-04-07T07:46:34Z")

</div>

I think the issue here is that `readline` never stops blocking because you never send a new line. Everything seems to work as expected with `client.write('hello\n');`.

---

<div class="post-metadata">

**Author:** ![guille](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guille/32/7505_2.png) [@guille](https://discourse.julialang.org/u/guille)\
**Post date:** [April 7, 2022, 4:09pm UTC](https://discourse.julialang.org/t/closing-a-tcp-stream-behavior-difference-from-nodejs/79140/3 "2022-04-07T16:09:20Z")

</div>

Wow, yeah, you’re absolutely right! That was quite silly 🙂

Thanks !
