I have severe problems to listen to a port on which an UDP-Socket server sends packages.
I see the packages in Wireshark and my Python-Script can see the packages as well.
But I struggle to achieve the same in Julia.
Here a short snippet, that does not work. The process stucks in line data, addr = recvfrom(sock).
using Sockets
sock = Sockets.UDPSocket()
Base.bind(sock, ip"0.0.0.0", 59000)
println("Socket bound. Waiting for packet (blocking)...")
addr, data = Sockets.recvfrom(sock) # direct, synchron, no async
println("✅ Received $(length(data)) bytes from $addr")
println(" Data: $(String(copy(data)))")
Base.close(sock)
Any ideas?
It works fine for me locally (same host). I sent the test packet with
echo -n "hello" | nc -u -w1 127.0.0.1 59000
Also from remote:
echo -n "hello" | nc -u -w1 dell.local 59000
both commands do not work for me.
And what would be the purpose?
This was in Ubuntu, are you using Windows or Mac if nc is not available? Someone using them could comment.
The following works
(AI Proton Lumo with my strong choaching
)
using Sockets
ch = Channel{Tuple{Union{Nothing, Sockets.InetAddr}, Union{Nothing, Vector{UInt8}}}}(1)
udp_sock = Sockets.UDPSocket()
Base.bind(udp_sock, ip"0.0.0.0", 59000; reuseaddr=true)
# Voll qualifiziert: Base.Threads.@spawn
recv_task = Base.Threads.@spawn begin
try
addr, data = recvfrom(udp_sock)
put!(ch, (addr, data))
catch
put!(ch, (nothing, nothing))
end
end
println(ch)
addr, data = take!(ch)
close(ch)
println(data)
Base.close(udp_sock)
@info "done"
P.S.:
There exist an alternative to encapsulate the troublemaker: addr, data = recvfrom(udp_sock):
recv_task = Base.Threads.@async while running[]
# ...
end
Any arguments to decide which of them is the best, to enable a sound handling of InterruptException?