# Read-all echo TCP server

**URL:** <https://discourse.julialang.org/t/read-all-echo-tcp-server/47107>\
**Category:** General Usage\
**Tags:** question, sockets\
**Created:** [September 23, 2020, 3:02am UTC](https://discourse.julialang.org/t/read-all-echo-tcp-server/47107 "2020-09-23T03:02:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![julbinb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julbinb/32/3079_2.png) [@julbinb](https://discourse.julialang.org/u/julbinb)\
**Post date:** [September 23, 2020, 3:02am UTC](https://discourse.julialang.org/t/read-all-echo-tcp-server/47107/1 "2020-09-23T03:02:36Z")

</div>

I need to write a Julia server that reads the entire message coming from a client, and then sends a reply back. However, it seems that when the message is read, the socket gets closed, so I can’t write the reply.

Server code:

```julia
using Sockets

port = 5678
server = listen(port)
sock = accept(server)

while isopen(sock)
  m = read(sock, String)
  write(sock, m)
end

```

Client:

```julia
$ cat input.txt | nc localhost 5678

```

On `write`, the server raises the error “IOError stream is closed or unusable”.

Is there a way to make `sock` not close too soon?

---

<div class="post-metadata">

**Author:** ![jerlich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerlich/32/5205_2.png) [@jerlich](https://discourse.julialang.org/u/jerlich)\
**Post date:** [September 23, 2020, 3:57am UTC](https://discourse.julialang.org/t/read-all-echo-tcp-server/47107/2 "2020-09-23T03:57:58Z")

</div>

Not sure why you are getting this error, but the examples in the manual is as follows. Maybe that `keep=true` option is important?

```julia
julia> @async begin
           server = listen(2001)
           while true
               sock = accept(server)
               @async while isopen(sock)
                   write(sock, readline(sock, keep=true))
               end
           end
       end
Task (runnable) @0x00007fd31dc12e60

julia> clientside = connect(2001)
TCPSocket(RawFD(28) open, 0 bytes waiting)

julia> @async while isopen(clientside)
           write(stdout, readline(clientside, keep=true))
       end
Task (runnable) @0x00007fd31dc11870

julia> println(clientside,"Hello World from the Echo Server")
Hello World from the Echo Server

```

---

<div class="post-metadata">

**Author:** ![julbinb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julbinb/32/3079_2.png) [@julbinb](https://discourse.julialang.org/u/julbinb)\
**Post date:** [September 23, 2020, 4:16am UTC](https://discourse.julialang.org/t/read-all-echo-tcp-server/47107/3 "2020-09-23T04:16:08Z")

</div>

The server indeed works with a client like that, but doesn’t work with my netcat client, which provides limited input stream to the server.

The `keep` parameter is for keeping end-of-line symbols if I am not mistaken.

---

<div class="post-metadata">

**Author:** ![jerlich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jerlich/32/5205_2.png) [@jerlich](https://discourse.julialang.org/u/jerlich)\
**Post date:** [September 23, 2020, 4:17am UTC](https://discourse.julialang.org/t/read-all-echo-tcp-server/47107/4 "2020-09-23T04:17:58Z")

</div>

I think the issue is with netcat. See this stack exchange question:  
[https://unix.stackexchange.com/questions/332163/netcat-send-text-to-echo-service-read-reply-then-exit](https://unix.stackexchange.com/questions/332163/netcat-send-text-to-echo-service-read-reply-then-exit)

---

<div class="post-metadata">

**Author:** ![julbinb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julbinb/32/3079_2.png) [@julbinb](https://discourse.julialang.org/u/julbinb)\
**Post date:** [September 23, 2020, 7:29pm UTC](https://discourse.julialang.org/t/read-all-echo-tcp-server/47107/5 "2020-09-23T19:29:25Z")

</div>

I am not sure ☹ The problem seems to be in that Julia’s socket becomes closed in both directions after netcat closes just one side. After one read, `sock` becomes unusable and does not let to write into it.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [September 23, 2020, 8:57pm UTC](https://discourse.julialang.org/t/read-all-echo-tcp-server/47107/6 "2020-09-23T20:57:15Z")

</div>

That certainly sounds like a bug, I would recommend filing an issue on the Julia issue tracker with your MWE.

---

<div class="post-metadata">

**Author:** ![julbinb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julbinb/32/3079_2.png) [@julbinb](https://discourse.julialang.org/u/julbinb)\
**Post date:** [September 23, 2020, 9:09pm UTC](https://discourse.julialang.org/t/read-all-echo-tcp-server/47107/7 "2020-09-23T21:09:15Z")

</div>

Thanks, I will. The socket becomes closed after any call to `read`…

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [September 23, 2020, 10:06pm UTC](https://discourse.julialang.org/t/read-all-echo-tcp-server/47107/8 "2020-09-23T22:06:45Z")

</div>

Out of curiosity, are you using it in a scripting environment (Atom, VSCode, Jupyter/Pluto notebooks) or running it as a script?

I had an issue like this a few years back when I tried to run code similar to this in a dynamic setting, but when it was ran as a script it somehow all worked just fine.

---

<div class="post-metadata">

**Author:** ![julbinb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julbinb/32/3079_2.png) [@julbinb](https://discourse.julialang.org/u/julbinb)\
**Post date:** [September 23, 2020, 10:23pm UTC](https://discourse.julialang.org/t/read-all-echo-tcp-server/47107/9 "2020-09-23T22:23:50Z")

</div>

Running as a script.

I have a program that reads stuff from IO till EOF and produces the result. And I’d like to simply turn this program into a simple server, smth like

```julia
server = listen(port)
sock = accept(server)

main(in::IO, out::IO) = ...
main(sock, sock)

close(sock)

```
