Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR ERLANG

tcp server erlang

server() ->
    {ok, LSock} = gen_tcp:listen(5678, [binary, {packet, 0}, {active, false}]),
    {ok, Sock} = gen_tcp:accept(LSock),
    {ok, Bin} = do_recv(Sock, []),
    ok = gen_tcp:close(Sock),
    ok = gen_tcp:close(LSock),
    Bin.

do_recv(Sock, Bs) ->
    case gen_tcp:recv(Sock, 0) of
        {ok, B} ->
            do_recv(Sock, [Bs, B]);
        {error, closed} ->
            {ok, list_to_binary(Bs)}
    end.
Source by www.erlang.org #
 
PREVIOUS NEXT
Tagged: #tcp #server #erlang
ADD COMMENT
Topic
Name
2+3 =