# How to initialize \`UDPSocket\` for broadcast?

**URL:** https://discourse.julialang.org/t/how-to-initialize-udpsocket-for-broadcast/17149
**Category:** General Usage
**Tags:** networking
**Created:** [November 4, 2018, 11:58pm UTC](https://discourse.julialang.org/t/how-to-initialize-udpsocket-for-broadcast/17149 "2018-11-04T23:58:37Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![standarddeviant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/standarddeviant/32/2309_2.png) [@standarddeviant](https://discourse.julialang.org/u/standarddeviant)
#### Post date: [November 4, 2018, 11:58pm UTC](https://discourse.julialang.org/t/how-to-initialize-udpsocket-for-broadcast/17149/1 "2018-11-04T23:58:37Z")

</div>

What is the correct way to initialize a `UDPSocket` for only sending broadcast packets?

I wondered if this would work:

```julia
julia> usock=UDPSocket()
UDPSocket(init)

julia> send(usock, ip"255.255.255.255", 42000, "test")
ERROR: IOError: send: permission denied (EACCES)
Stacktrace:
 [1] uv_error at .\libuv.jl:85 [inlined]
 [2] send(::UDPSocket, ::IPv4, ::Int64, ::String) at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\Sockets\src\Sockets.jl:354
 [3] top-level scope at none:0

```

And that error makes sense. On v1.0.1, I tried to use `Sockets.setopt` and that yields a different issue:

```julia
julia> usock=UDPSocket()
UDPSocket(init)

julia> Sockets.setopt(usock; enable_broadcast=true)
ERROR: IOError: enable_broadcast: bad file descriptor (EBADF)
Stacktrace:
 [1] uv_error at .\libuv.jl:85 [inlined]
 [2] #setopt#4 at C:\cygwin\home\Administrator\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.0\Sockets\src\Sockets.jl:266 [inlined]
 [3] (::getfield(Sockets, Symbol("#kw##setopt")))(::NamedTuple{(:enable_broadcast,),Tuple{Bool}}, ::typeof(Sockets.setopt), ::UDPSocket) at .\none:0
 [4] top-level scope at none:0

```

I can get around this by:

1. Making a `UDPSocket`
2. Sending a ‘throw-away’ packet to localhost
3. Using `Sockets.setopt`, set `enable_broadcast = true`
4. Finally send a packet on the broadcast address

I’ve confirmed this works (see below), but is this the preferred way to actually set up and use a UDPSocket for broadcast?

On the receive side:

```julia
julia> using Sockets

julia> usock=UDPSocket()
UDPSocket(init)

julia> bind(usock, ip"0.0.0.0", 42000)
true

julia> apkt = String(recv(usock))
"actual_payload"

julia>

```

On the send side:

```julia
julia> using Sockets

julia> usock=UDPSocket()
UDPSocket(init)

julia> send(usock, ip"127.0.0.1", 0, "test")

julia> Sockets.setopt(usock; enable_broadcast=true)

julia> send(usock, ip"255.255.255.255", 42000, "actual_payload")

julia>

```

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [November 5, 2018, 12:45am UTC](https://discourse.julialang.org/t/how-to-initialize-udpsocket-for-broadcast/17149/2 "2018-11-05T00:45:01Z")

</div>

You can use a `bind()` in place of the throw away packet, `setopt` needs an initialized socket i think

---

<div class="post-metadata">

### Author: ![standarddeviant](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/standarddeviant/32/2309_2.png) [@standarddeviant](https://discourse.julialang.org/u/standarddeviant)
#### Post date: [November 5, 2018, 3:49pm UTC](https://discourse.julialang.org/t/how-to-initialize-udpsocket-for-broadcast/17149/3 "2018-11-05T15:49:34Z")

</div>

Can multiple applications bind to the same UDP socket?

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [November 6, 2018, 1:04am UTC](https://discourse.julialang.org/t/how-to-initialize-udpsocket-for-broadcast/17149/4 "2018-11-06T01:04:09Z")

</div>

There’s a `reuseaddr = false` as the default, and it says only the last to bind to an address receives traffic, so they should be transmit only but yes  
see the `>?bind` last paragraph for a bit more info
