使用Julia设置UDP [英] Setting up UDP with Julia

查看:107
本文介绍了使用Julia设置UDP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Julia在LAN中设置UDP通信接口?我似乎在Julia回购中识别了Base/socket.jl,但是几乎没有任何有关如何使用这些功能的文档.

How to setup UDP communication interface in a LAN using Julia? I seem to recognize Base/socket.jl in the Julia repo but there is hardly any documentation on how to use the functions.

推荐答案

是的,有关UDP的文档现在仍然缺失,但是也许我们可以通过

yeah, documentation on UDP is still missing now, but maybe we can learn how to use UDP via the TCP example. Here is my testing code.

julia> VERSION
v"0.4.0-dev+6494"

julia> @async begin
         udpsock = UDPSocket()
         bind(udpsock,ip"127.0.0.1",2000)
         while true
           println(bytestring(recv(udpsock)))
         end
       end
Task (waiting) @0x000000010c2a1900

julia> sock = UDPSocket()
UDPSocket(Ptr{Void} @0x00007f91e430d530,1,Condition(Any[]),Condition(Any[]),Condition(Any[]))

julia> send(sock,ip"127.0.0.1",2000,"Hello World from the UDP")
Hello World from the UDP

请注意,如果您使用的是julia v0.3.*,则应使用UdpSocket()而不是UDPSocket().

note that, if you are using julia v0.3.*, you should use UdpSocket() instead of UDPSocket().

更新:我忘了初始化(使用bind())套接字,这导致close(sock)抛出错误.

UPDATE: I forgot to initialize(use bind()) the socket, which results in close(sock) throwing an error.

julia> sock = UDPSocket()
UDPSocket(Ptr{Void} @0x00007f91e430d530,1,Condition(Any[]),Condition(Any[]),Condition(Any[]))

julia> bind(sock,ip"127.0.0.1",1000)
true

julia> send(sock,ip"127.0.0.1",2000,"Hello World from the UDP")
Hello World from the UDP

julia> close(sock)

这篇关于使用Julia设置UDP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆