N3sh
N3sh

Reputation: 878

Erlang - Sending message to UDP Multicast Session

I am currently working on a way to send packets to a UDP multicast session.

Here is my current code (just listening):

-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.

It basically listens to the specified multicast IP and outputs From, Port and the Data.

The goal is to be able to send some packets back as well.

Upvotes: 0

Views: 359

Answers (1)

new7877
new7877

Reputation: 87

in cpp:

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

...

in erlang

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

...

it`s simple,u must know the protocol in either language. when u receive "AnyThingElse" in erlang, just

<<data1:32, data2:16, data3:8, ....>> = AnyThingElse,

in erlang it`s simple to use binary data. i love erlang

Upvotes: 2

Related Questions