Erlang - 向UDP多播会话发送消息

时间:2013-02-22 10:02:46

标签: c++ erlang udp multicast

我目前正致力于将数据包发送到UDP多播会话。

这是我当前的代码(只是在听):

-module(zcclient).

-export([open/2,start/0]).
-export([stop/1,receiver/0]).

open(Addr,Port) ->
   {ok,S} = gen_udp:open(Port,[{reuseaddr,true}, {ip,Addr}, {multicast_ttl,4}, {multicast_loop,fa$
   inet:setopts(S,[{add_membership,{Addr,{225,0,0,111}}}]),
   S.

close(S) -> gen_udp:close(S).

start() ->
   S=open({225,0,0,111},12175),
   Pid=spawn(?MODULE,receiver,[]),
   gen_udp:controlling_process(S,Pid),
   {S,Pid}.

stop({S,Pid}) ->
   close(S),
   Pid ! stop.

receiver() ->
   receive
       {udp, _Socket, IP, InPortNo, Packet} ->
           io:format("~n~nFrom: ~p~nPort: ~p~nData: ~p~n",[IP,InPortNo,Packet]),
           receiver();
       stop -> true;
       AnythingElse -> io:format("RECEIVED: ~p~n",[AnythingElse]),
           receiver()
   end.

它基本上监听指定的组播IP并输出From,Port和Data。

目标是能够发回一些数据包。

1 个答案:

答案 0 :(得分:2)

在cpp:

struct student{ int32 id; int32 grade; int32 class;};

...

在erlang中

<<id:32, grade:32, class:32>>

...

这很简单,你必须知道任何一种语言的协议。 当你在erlang中收到“AnyThingElse”时,只需

<<data1:32, data2:16, data3:8, ....>> = AnyThingElse,
在erlang中,使用二进制数据很简单。我爱二郎