# Is there a \`getsockname\` for \`UDPSocket\`?

**URL:** <https://discourse.julialang.org/t/is-there-a-getsockname-for-udpsocket/122553>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 12, 2024, 3:48pm UTC](https://discourse.julialang.org/t/is-there-a-getsockname-for-udpsocket/122553 "2024-11-12T15:48:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pedro](https://avatars.discourse-cdn.com/v4/letter/p/919ad9/32.png) [@Pedro](https://discourse.julialang.org/u/Pedro)\
**Post date:** [November 12, 2024, 3:48pm UTC](https://discourse.julialang.org/t/is-there-a-getsockname-for-udpsocket/122553/1 "2024-11-12T15:48:03Z")

</div>

I’m trying to write some simple program that involves communication with UDP sockets. I would like not to manually bind the port to the socket, and let the OS choose it. It can be achieved by binding to port `0`. However, I do not see a way to retrieve the port number from the `UDPSocket` struct. Function `getsockname` that can do it is exclusively for `TCPSocket`.

```julia
using Sockets

socket = UDPSocket()
bind(socket, ip"0.0.0.0", 0)

getsockname(socket) # It only works for TCP sockets

close(socket)

```

Is there any proper way to retrieve the port? I only have hacky approaches, that involves sending one packet to second socket, but that’s just bad. 🙂

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [November 12, 2024, 8:31pm UTC](https://discourse.julialang.org/t/is-there-a-getsockname-for-udpsocket/122553/2 "2024-11-12T20:31:13Z")

</div>

Looks like the wrapper for `uv_udp_getsockname` is missing, the TCP one is [here](https://github.com/JuliaLang/julia/blob/master/src/jl_uv.c#L870). As a (possibly worse than what you are doing) hack, you can call it directly:

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

julia> bind(socket, ip"0.0.0.0", 0)
true

julia> size = Ref{Int64}(16)
Base.RefValue{Int64}(16)

julia> sockaddr = zeros(UInt16, 8)
8-element Vector{UInt16}:
 0x0000
 0x0000
 0x0000
 0x0000
 0x0000
 0x0000
 0x0000
 0x0000

julia> @ccall uv_udp_getsockname(socket.handle::Ptr{Cvoid}, sockaddr::Ptr{Cvoid}, size::Ptr{Int64})::Cint
0

julia> Int(ntoh(sockaddr[2]))
54592

```

---

<div class="post-metadata">

**Author:** ![Pedro](https://avatars.discourse-cdn.com/v4/letter/p/919ad9/32.png) [@Pedro](https://discourse.julialang.org/u/Pedro)\
**Post date:** [November 13, 2024, 10:14am UTC](https://discourse.julialang.org/t/is-there-a-getsockname-for-udpsocket/122553/3 "2024-11-13T10:14:08Z")

</div>

That’s a bummer it is missing.

The solution your propose is nice, certainly better than what I did, thanks. 😃

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 13, 2024, 12:31pm UTC](https://discourse.julialang.org/t/is-there-a-getsockname-for-udpsocket/122553/4 "2024-11-13T12:31:12Z")

</div>

Can you create an issue at: [GitHub · Where software is built](https://github.com/JuliaLang/julia/issues) so that the developers are informed that this should be added?
