Properly connect Julia processes in multiple machines with Sockets

Using the Sockets and Serialization libraries, I’m able to connect two Julia processes and exchange data between them. To connect two Julia processes in the same machine I can do the following:

Julia process A

sckt = accept(listen(5000)) # listens to port 5000

Julia process B

sckt = connect(5000)

When trying to repeat the same in two different computers:

Julia machine A

sckt = accept(listen(5000)) # listens to port 5000

Julia machine B

sckt = connect(ip_of_A, 5000)

machine B throws the following error: ERROR: IOError: connect: connection timed out (ETIMEDOUT)

My knowledge on Sockets/TCP is limited, so I’m sure I’m doing something wrong… I’m almost certain that the way I use Sockets to connect processes in the same machine is not the most appropriate either.

See the documentation here: Sockets · The Julia Language

By default, it only listens on the local network interface. To accept connections from remote machines, you should listen on all interfaces as described.

1 Like

I was looking at the documentation but it was hard for me to interpret. Based on your comment I tried a few things more and found that the following was working:

Julia machine A

sckt = accept(listen(IPv4(0), 5000))

Julia machine B

sckt = connect(ip_of_A, 5000)

The IPv4(0) means that the machine is waiting for a connection request from any IP? Or how should I interpret this?

IPv4(0) would listen on the “0.0.0.0” which translates to ALL IPv4 interfaces. So if your computer has multiple network cards, it will listen on all of them. Which means that any computer that can reach your computer over the network can connect to it on port 5000.

If you are running linux you can run ip addr and it will list all the “network interfaces”. On my computer with only 1 network card I have 5 “network interfaces”. 4 of those are internal and only 1 connects to the actual network card (on the motherboard).

If you are on a laptop you probably have 2 network cards, the wired and the wireless, and you probably have more network interfaces, they are often used for VPN and internal routing and what not.

Taking the wired/wireless configuration which is fairly common. If you listen on IPv4(0) that means a connection coming over your wireless network card OR your wired card will connect to your computer.

Now lets say your wired card has an IP address of “192.168.56.10” and your wireless card has an IP address of “192.168.56.50”. They are both on the same network, and your computer gets to decide which one it wants to use when you access a web site. In this case you could listen on ip"192.168.56.10" (or parse(IPAddr, "192.168.56.10")). This means Julia will only listen for incoming connections over your wired card. If a computer tries to connect to your computer via the wireless IP address it will be denied.

And with dhcp if your IP address changes because you rebooted your router, the listen() call will fail if your computer no longer has the 192.168.56.10 IP address.

Hopefully that all makes sense.

3 Likes

Now everything makes more sense, thank you!