Hey @matt,
I think I found the solution to your issue. I’m definitely not a socket expert, but I was able to get your example working. Short version is, the kernel has a TIME_WAIT
state when it is closing a TCP connection graceful, meaning you can’t reuse it until that is finished. Normally in C an any easy way to “bypass” this “problem” is to set the SO_REUSEADDR
socket option to true, which you won’t have access to in the net
package. Luckily, the net
package is kind of enough to set this for us on a TCPListener as in your example application by default. Obviously, you still get the bind: address already in use
error, which is SO answer explains in depth as to why this still happens, even though SO_REUSEADDR
is enabled on the socket.
TLDR; either use straight syscalls so you can enable SO_REUSEPORT
on your socket, which isn’t portable to all systems. Or the better solution is to bind the parent process to 0.0.0.0:1234
and the child to 192.168.0.1:1234
(or whatever the machine IP is).