Example code:
using DataStructures
# disable SIG_INT
println("To termiate the logger, press <CTRL>+<C>!")
ccall(:jl_exit_on_sigint, Void, (Cint,), 0)
# use an array with one element for global variables, such that they can be declared as const
const terminate = [false]
# create a queue for buffering incoming messages
const q = Queue(String)
# background loop for receiving UDP messages, terminated with <ctrl> + <c>
function receiveData(server_ip)
global terminate
udpsock = UDPSocket()
# setopt(udpsock, enable_broadcast=true)
bind(udpsock, server_ip, port)
i = 0
try
while true
msg = String(copy(recv(udpsock)))
enqueue!(q, msg)
end
catch my_exception
if isa(my_exception, InterruptException)
close(udpsock)
terminate[1] = true
end
end
end
server_ip = IPv4("0.0.0.0")
@async receiveData(server_ip) # start the background task of receiving UDP packages