# Properly connect Julia processes in multiple machines with Sockets

**URL:** <https://discourse.julialang.org/t/properly-connect-julia-processes-in-multiple-machines-with-sockets/38487>\
**Category:** New to Julia\
**Tags:** question, sockets\
**Created:** [April 30, 2020, 2:55pm UTC](https://discourse.julialang.org/t/properly-connect-julia-processes-in-multiple-machines-with-sockets/38487 "2020-04-30T14:55:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [April 30, 2020, 2:55pm UTC](https://discourse.julialang.org/t/properly-connect-julia-processes-in-multiple-machines-with-sockets/38487/1 "2020-04-30T14:55:08Z")

</div>

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**

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

```

**Julia process B**

```julia
sckt = connect(5000)

```

When trying to repeat the same in two different computers:

**Julia machine A**

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

```

**Julia machine B**

```julia
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.

---

<div class="post-metadata">

**Author:** ![robsmith11](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robsmith11/32/29641_2.png) [@robsmith11](https://discourse.julialang.org/u/robsmith11)\
**Post date:** [April 30, 2020, 3:42pm UTC](https://discourse.julialang.org/t/properly-connect-julia-processes-in-multiple-machines-with-sockets/38487/2 "2020-04-30T15:42:13Z")

</div>

See the documentation here: [Sockets · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Sockets/#Sockets.listen-Tuple%7BAny%7D)

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

---

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [April 30, 2020, 4:24pm UTC](https://discourse.julialang.org/t/properly-connect-julia-processes-in-multiple-machines-with-sockets/38487/3 "2020-04-30T16:24:35Z")

</div>

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**

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

```

**Julia machine B**

```julia
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?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [April 30, 2020, 4:47pm UTC](https://discourse.julialang.org/t/properly-connect-julia-processes-in-multiple-machines-with-sockets/38487/4 "2020-04-30T16:47:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [April 30, 2020, 5:00pm UTC](https://discourse.julialang.org/t/properly-connect-julia-processes-in-multiple-machines-with-sockets/38487/5 "2020-04-30T17:00:34Z")

</div>

Now everything makes more sense, thank you!
