문제

멀티캐스팅을 수행하기 위해 Erlang에서 gen_udp를 어떻게 사용합니까?나는 그것이 코드에 있다는 것을 알고 있지만 그 뒤에는 문서가 없습니다.데이터를 보내는 것은 분명하고 간단합니다.회원가입 방법이 궁금합니다.시작 시 멤버십을 추가하는 것뿐만 아니라, 런닝 중에도 멤버십을 추가하는 것도 유용할 것 같아요.

도움이 되었습니까?

해결책

다음은 Bonjour/Zeroconf 트래픽을 수신하는 방법에 대한 예제 코드입니다.

-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,false}, binary]),
   inet:setopts(S,[{add_membership,{Addr,{0,0,0,0}}}]),
   S.

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

start() ->
   S=open({224,0,0,251},5353),
   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,inet_dns:decode(Packet)]),
           receiver();
       stop -> true;
       AnythingElse -> io:format("RECEIVED: ~p~n",[AnythingElse]),
           receiver()
   end. 

다른 팁

멀티캐스트 전송에 응답했습니다. 수신하려면 멀티캐스트 그룹에 가입해야 합니다.

(여전히) 문서화되지 않은 것처럼 보이지만 이전에 erlang-questions 메일링 리스트에서 다뤄진 적이 있습니다. http://www.erlang.org/pipermail/erlang-questions/2003-March/008071.html

    {ok, Socket} = gen_udp:open(Port, [binary, {active, false},
                                       {reuseaddr, true},{ip, Addr}, 
                                       {add_membership, {Addr, LAddr}}]).

어디에 Addr 멀티캐스트 그룹이고 LAddr 로컬 인터페이스입니다.(코드 제공: mog)

위에서 사용한 것과 동일한 옵션을 다음으로 전달할 수 있습니다. inet:setopts 포함 {drop_membership, {Addr, LAddr}} 그룹의 말을 듣지 않으려면

나는 이 예제를 내 PC에서 실행하려고 노력합니다.수신 소켓을 열 때 항상 {error,eaddrnotavail} 메시지가 표시되면 어떻게 되나요?

예시 1:이것은 작동합니다:

{ok, Socket} = gen_udp:open(?PORT, [{reuseaddr,true}, {ip,?SERVER_IP},
               {multicast_ttl,4}, {multicast_loop,false}, binary]),

예시 2:런타임 오류 발생:

{ok, Socket} = gen_udp:open(?PORT, [{reuseaddr,true}, {ip,?MULTICAST_IP},
               {multicast_ttl,4}, {multicast_loop,false}, binary]),

% --> {오류,eaddrnotavail}

-define(SERVER_IP, {10,31,123,123}). % The IP of the current computer
-define(PORT, 5353).
-define(MULTICAST_IP, {224,0,0,251}). 

멀티캐스트는 IP 주소로 지정됩니다.

모든 언어와 마찬가지로 erlang에서도 동일합니다.IP 주소 224.0.0.0부터 239.255.255.255까지가 멀티캐스트 주소입니다.

해당 범위에서 주소를 선택하고 이미 할당된 주소와 겹치지 않는지 확인하면 됩니다.

http://www.iana.org/designments/multicast-addresses

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top