Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Change Sockets.recvfrom to return full InetAddr instead of just host #32729

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Standard library changes
* `clamp` can now handle missing values ([#31066]).
* `empty` now accepts a `NamedTuple` ([#32534]).
* `mod` now accepts a unit range as the second argument to easily perform offset modular arithmetic to ensure the result is inside the range ([#32628]).
* `Sockets.recvfrom` now returns both host and port as an InetAddr ([#32729]).

#### Libdl

Expand Down
12 changes: 8 additions & 4 deletions stdlib/Sockets/src/Sockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,14 @@ function recv(sock::UDPSocket)
end

"""
recvfrom(socket::UDPSocket) -> (address, data)
recvfrom(socket::UDPSocket) -> (host_port, data)

Read a UDP packet from the specified socket, returning a tuple of `(host_port, data)`, where
`host_port` will be an InetAddr{IPv4} or InetAddr{IPv6}, as appropriate.

!!! compat "Julia 1.3"
This method requires at least Julia 1.3.

Read a UDP packet from the specified socket, returning a tuple of `(address, data)`, where
`address` will be either IPv4 or IPv6 as appropriate.
"""
function recvfrom(sock::UDPSocket)
iolock_begin()
Expand All @@ -348,7 +352,7 @@ function recvfrom(sock::UDPSocket)
From = Union{InetAddr{IPv4}, InetAddr{IPv6}}
Data = Vector{UInt8}
from, data = wait(sock.recvnotify)::Tuple{From, Data}
return (from.host, data)
return (from, data)
finally
unlock(sock.recvnotify)
end
Expand Down
8 changes: 4 additions & 4 deletions stdlib/Sockets/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ end
@test fetch(tsk) == msg
end
let tsk = @async send(b, ip"127.0.0.1", randport, "WORLD HELLO")
(addr, data) = recvfrom(a)
@test addr == ip"127.0.0.1" && String(data) == "WORLD HELLO"
(inetaddr, data) = recvfrom(a)
@test inetaddr.host == ip"127.0.0.1" && String(data) == "WORLD HELLO"
wait(tsk)
end
close(a)
Expand All @@ -302,8 +302,8 @@ end

for i = 1:3
tsk = @async begin
let (addr, data) = recvfrom(a)
@test addr == ip"::1"
let (inetaddr, data) = recvfrom(a)
@test inetaddr.host == ip"::1"
@test String(data) == "Hello World"
end
end
Expand Down