【问题标题】:can't get data via gen_udp无法通过 gen_udp 获取数据
【发布时间】:2016-11-14 08:56:44
【问题描述】:

我在远程服务器上运行tcpdump -i eth0 -vv ip6 并查看此类数据包

11:10:36.712804 IP6 (hlim 1, next-header UDP (17) payload length: 103) fe80::f816:3eff:fe94:a348.57100 > ff3c::8500:2345.57100: [udp sum ok] UDP, length 95

但是当我打开 Socket 时

{ok, Socket} = gen_udp:open(57100, [binary, {active, true}, {ip, any}, inet6, {reuseaddr, true}])

{ok, Socket} = gen_udp:open(57100, [binary, {active, true}, {ip, any}, inet6, {multicast_ttl, 225}, {multicast_loop, false}, {reuseaddr, true}])

{ok, Socket} = gen_udp:open(57100, [binary, {active, true}, {ip, {65340,0,0,0,0,0,34048,9029}}, inet6, {multicast_ttl, 225}, {multicast_loop, false}, {reuseaddr, true}])

{65340,0,0,0,0,0,34048,9029} = <<"FF3C:0000:0000:0000:0000:0000:8500:2345">>

并以这种方式等待消息

subscribe_on_stream() -> 
    %% {ok, Socket} = gen_udp:open(57100, [binary, {active, true}, {ip, IP}, inet6, {multicast_ttl, 225}, 
    %% {multicast_loop, false}, {reuseaddr, true}]), 
    {ok, Socket} = gen_udp:open(Port, [binary, {active, true}, {reuseaddr, true}]), 
    io:format("self: ~p line: ~p~n", [self(), ?LINE]), 
    subscribe_loop(Socket). 

subscribe_loop(Socket) -> 
    receive 
       Any -> 
           io:format("~p~n", [Any])
    end.

【问题讨论】:

  • 你能提供更多的代码吗? receive ... end. 放在哪里?
  • subscribe_on_stream() -> %% {ok, Socket} = gen_udp:open(57100, [binary, {active, true}, {ip, IP}, inet6, {multicast_ttl, 225}, %% {multicast_loop, false}, {reuseaddr, true}]), {ok, Socket} = gen_udp:open(Port, [binary, {active, true}, {reuseaddr, true}]), io:format("self: ~p line: ~p~n", [self(), ?LINE]), subscribe_loop(Socket). subscribe_loop(Socket) -> receive Any -> io:format("~p~n", [Any]) end.
  • @ipinak 我在其他进程中获取资源并没有出错
  • can't add multicast group的可能重复

标签: sockets erlang udp


【解决方案1】:

问题出在subscribe_loop(Socket)。它实际上不是循环的。您调用该函数一次,它就会返回,仅此而已。您需要递归调用subscribe_loop(...) 进行循环。

这是一个如何编写一个非常简单的服务器等待消息的示例。

https://www.erlang.org/course/concurrent-programming

解决办法:

subscribe_loop(Socket) -> 
    receive 
       Any -> 
           io:format("~p~n", [Any]),
           subscribe_loop(Socket)
    end.

【讨论】:

  • 毫米。不。如果你的意思是正确的,我会收到至少一条消息,但我没有
  • 问题是我没有收到至少一条消息
  • 你是对的,你必须在它死之前至少收到一条消息。我刚刚运行了完全相同的代码并且它工作正常。所以还有其他事情发生。检查你的防火墙,也许你拒绝了对 erlang 的访问。
  • 是的,问题要深得多。我会尽力解决的
  • 您有收听多播的经验吗?我想问题可能是我以错误的方式发送它 gen_udp:open(57100, [binary, {active, true}, {ip, {65340,0,0,0,0,0,34048,9029}}, inet6 , {multicast_ttl, 225}, {multicast_loop, false}, {reuseaddr, true}])
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
相关资源
最近更新 更多