# Listen on both IPv4 and IPv6

**URL:** https://discourse.julialang.org/t/listen-on-both-ipv4-and-ipv6/123651
**Category:** General Usage
**Tags:** networking
**Created:** [December 10, 2024, 8:28am UTC](https://discourse.julialang.org/t/listen-on-both-ipv4-and-ipv6/123651 "2024-12-10T08:28:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)
#### Post date: [December 10, 2024, 8:28am UTC](https://discourse.julialang.org/t/listen-on-both-ipv4-and-ipv6/123651/1 "2024-12-10T08:28:36Z")

</div>

I’m trying to listen on both IPv4 and IPv6.

```julia
  ear4=listen(IPv4(0),port)
  ear6=listen(IPv6(0),port)

```

This resulted in `IOError: listen: address already in use (EADDRINUSE)` on the IPv6 line. I’m running Linux, and if I remember right from many years ago when I tried in C or C++ to do some networking, listening on IPv6 also listens on IPv4, but on BSD (I tried DragonFly) it doesn’t.

How should I do this? Should I listen on IPv6 first, then `try` IPv4 and note whether it succeeded?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [December 10, 2024, 9:20am UTC](https://discourse.julialang.org/t/listen-on-both-ipv4-and-ipv6/123651/2 "2024-12-10T09:20:32Z")

</div>

By default, when you listen on IPv6, the server runs in dual stack mode so the following listens on IPv4 too:

```julia
using Sockets

port = 8080

server = Sockets.TCPServer()
bind(server, ip"::", port)
listen(server)

```

If you want them separate (like in your example) you need to disable dual stack mode when listening on IPv6. There is a keyword argument to `bind` which enables this, but that argument isn’t surfaced up to `listen` (perhaps it should, would be an easy contribution) so you need to do something like this:

```julia
using Sockets

port = 8080

# IPv4 (this could just as well be `srv4 = listen(ip"0", port)`)
srv4 = Sockets.TCPServer()
bind(srv4, ip"0", port)
listen(ip"0", port)

# IPv6
srv6 = Sockets.TCPServer()
bind(srv6, ip"::", port, ipv6only = true)
listen(srv6)

```

---

<div class="post-metadata">

### Author: ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)
#### Post date: [December 10, 2024, 9:32am UTC](https://discourse.julialang.org/t/listen-on-both-ipv4-and-ipv6/123651/3 "2024-12-10T09:32:43Z")

</div>

> [@fredrikekre](#):
>
> By default, when you listen on IPv6, the server runs in dual stack mode

Which operating system? Have you tried it on BSD?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [December 10, 2024, 9:36am UTC](https://discourse.julialang.org/t/listen-on-both-ipv4-and-ipv6/123651/4 "2024-12-10T09:36:18Z")

</div>

No this was on Linux.

---

<div class="post-metadata">

### Author: ![phma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phma/32/6576_2.png) [@phma](https://discourse.julialang.org/u/phma)
#### Post date: [December 10, 2024, 9:43am UTC](https://discourse.julialang.org/t/listen-on-both-ipv4-and-ipv6/123651/5 "2024-12-10T09:43:39Z")

</div>

Okay, I’ll assume that BSD doesn’t do dual stack. I should try both `listen`s, in case someone else bound the port (maybe two copies of the program are running).
