[erlang-questions] Line Receiver example?
Jarrod Roberson
jarrod@REDACTED
Mon Apr 27 21:12:29 CEST 2009
2009/4/27 Oscar Hellström <oscar@REDACTED>
> Hi Jarrod,
>
> I guess what you're looking for is the packet option for sockets. In
> case you're using a TCP socket you can either open your listen socket
> with the option {packet, line}, gen_tcp:listen(Port, [{packet, line}]),
> or you can set it to line based parsing later by issuing
> inet:setopts(Socket, [{packet, line}]). In case of UDP you need to open
> the socket with gen_upd:open(Port, [{packet, line}]). inet:setopts/2
> will also work for a UDP socket.
>
>
thanks, but I still seem to be missing something.
I still can't get it to print out anything when I connect and send data to
my server.
I think something is wrong in my get_line/1 function.
Here is my code so far.
-module(linereceiver).
-export([start/0]).
-import(lists, [reverse/1]).
sleep(T) ->
receive
after T ->
true
end.
start() ->
spawn(fun() ->
start_parallel_server(3000),
sleep(infinity)
end).
start_parallel_server(Port) ->
{ok, Listen} = gen_tcp:listen(Port, [binary, {packet,line},
{reuseaddr, true},
{active, true}]),
spawn(fun() -> par_connect(Listen)end).
par_connect(Listen) ->
{ok, Socket} = gen_tcp:accept(Listen),
spawn(fun() -> par_connect(Listen) end),
inet:setopts(Socket, [{packet, line}, binary, {nodelay, true}, {active,
true}]),
io:format("Connection Made!~n"),
get_line(Socket).
get_line(Socket) ->
receive
{tcp, Socket, Bin} ->
io:format("Received Line:~p~n", binary_to_list(Bin)),
get_line(Socket);
{tcp_closed, Socket} ->
io:format("Connection Closed!~n")
end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090427/b39ce319/attachment.htm>
More information about the erlang-questions
mailing list