-module(test1). -export([start_server/0]). start_server() -> spawn( fun() -> server(4000) end ). server(Port) -> {ok, Socket} = gen_udp:open( Port, [binary] ), io:format( "socked opened ~p~n ", [Socket]), loop(Socket). loop(Socket) -> receive {udp, Socket, Host, Port, Bin} -> Adr = binary_to_list(getadr(Bin)), io:format( "got: ~p~n", [{Host, Port, Adr}]), dispatch( Adr, Bin ), loop(Socket) end. %% extract osc event address getadr( Bin ) -> Len = msglen( Bin, 0 ), {Msg, _} = split_binary(Bin, Len), Msg. %% Get Length of the first \0 based string msglen(<<0, _/binary>>, Len) -> Len; msglen(Bin, Len) -> { _, Next} = split_binary( Bin, 1 ), msglen( Next, Len + 1). %% send message to host/port send( Host, Port, Bin) -> {ok, Socket} = gen_udp:open( 0, [binary] ), ok = gen_udp:send( Socket, Host, Port, Bin ), gen_udp:close( Socket ). %% define where to send the messages dispatch( "/osc/test" ++ _, Bin ) -> io:format( "TEST ~p~n", [Bin] ), send( "localhost", 4001, Bin); dispatch( "/osc/midi" ++ _, Bin ) -> io:format( "MIDI ~p~n", [Bin] ), send( "localhost", 4002, Bin); dispatch( _, _ ) -> io:format( "UNKNOWN ~n" ).