I have been trying to implement a simple TCP server that sends periodically data to a created connection. Based on the manual I can implement a minimal working example as follows:
function write_data(sock) while isopen(sock) write(sock,"hello\n") sleep(1) end println("socket closed") end errormonitor(@async begin server = listen(2001) while true sock = accept(server) println("Accepted connection") errormonitor(@async write_data(sock)) end end)
This works as expected. When I connect from different process, different language, it correctly accepts the connection and starts transmitting the data.
The problem I am having is when I close the connection from the different process (in julia
isopen(sock) still returns true thus the data are being still transmitted. This seems to be a wanted behaviour.
I have read upon the need to use
eof(sock) and tried to rewrite the function writing the data as
function write_data(sock) while isopen(sock) && !eof(sock) write(sock,"hello\n") sleep(1) end println("socket closed") end
But then it does not send anything as the call to
eof(sock) is blocking and the other end does not transmit anything.
What is the correct way to handle remote connection drop in such a loop where I want to only transmit data while the connection is active? Is there something such as non blocking eof?