I’m going to write a node that maintains network connections with other nodes. When the node is told to shut down, it has to finish sending and receiving messages, then shut down. This has to take a few seconds at most. (Julia 1.11.1)
First I create a listening socket:
julia> svr=listen(n) # I'm keeping the port number secret for now
Sockets.TCPServer(RawFD(25) active)
In another terminal, I connect to the port using netcat, then accept the connection in Julia:
julia> c2=accept(svr)
TCPSocket(RawFD(26) open, 0 bytes waiting)
First problem: how do I tell if there is a connection waiting on the listening socket? man accept
says it’s possible
In order to be notified of incoming connections on a socket, you can use select(2), poll(2), or epoll(7).
but how do I do this in Julia?
I sent the socket a few kilobytes. (First I tried eight bytes, aoeusnth
, then thought that’s a lot smaller than the MTU, so I tried about 5 kB.) Then I did bytesavailable(c2)
and got 0. I ran netstat -t
and saw that there were no bytes waiting in the receive or transmit queue. I did read(c2,4)
and got the first four bytes of the file. Then I did bytesavailable(c2)
and got 4910. The connection is on localhost, so the bytes should be available immediately, shouldn’t they?
Second problem: where are these bytes hiding, and how can I tell if there are some bytes hiding there? How to read from socket in non-blocking mode is relevant, but it’s from 2017-07, before Julia 1.0. If I knew that some bytes were hiding there, I could call read(c2,1)
and bytesavailable
would tell me how many more bytes there were.
In case it matters, the program will AFAIK run only on some sort of Unix.