[erlang-questions] Mini Server does not reciv

Schneider <>
Thu Aug 29 14:11:03 CEST 2013


Hi List,

I tried playing around with  a baby example of a server, just printing 
the data it receives.  But I don't get any output:

the Code:

> -module(mini_web_srv).
>
> -export([start/0,send/0]).
>
> start() ->
>     { ok, Listen } = gen_tcp:listen(8080,
>                    [binary,
>                     {packet, 4},
>                     {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),
>     loop(Socket).
>
> loop(Socket) ->
>     receive
>     {tcp, Socket, Bin} ->
>         io:format("received ~p~n",[Bin]),
>         loop(Socket);
>     {tcp_closed, _} ->
>         io:format("closed connection");
>     {tcp_error, Socket, Reason} ->
>         io:format("Error on socket ~p reason: ~p~n", [Socket, Reason])
>     end.
>
> send() ->
>     send_data("foo").
>
> send_data(Data) ->
>     {ok, Send} = gen_tcp:connect("localhost", 8080,[binary, {packet, 
> 0} ]),
>     gen_tcp:send(Send, Data). 

and the commands used in the EShell:

> Eshell V5.8.5  (abort with ^G)
> 1> c("/home/j_schn14/src/erlang/mini_web_srv/mini_web_srv", [{outdir, 
> "/home/j_schn14/src/erlang/mini_web_srv/"}]).
> c("/home/j_schn14/src/erlang/mini_web_srv/mini_web_srv", [{outdir, 
> "/home/j_schn14/src/erlang/mini_web_srv/"}]).
> {ok,mini_web_srv}
> 2> mini_web_srv:start().
> <0.39.0>
> 3>
> 3>
> 3> mini_web_srv:send().
> Accepted Connection on #Port<0.2075>
> ok
> 4>
> 4> {ok, SendSocket} = gen_tcp:connect("localhost", 8080, [binary, 
> {packet, 0} ]).
> Accepted Connection on #Port<0.2077>
> {ok,#Port<0.2076>}
> 5>
> 5> gen_tcp:send(SendSocket,<<"foo">>).
> ok
> 6>
> 6> gen_tcp:close(SendSocket).
> ok
> closed connection7>
> 7> 


I would expect after line 3 (to be exact, after the "Accepted Connection 
on #Port<0.2075>")  and after line 5 to get "received foo" e.g. 
"received <<foo>>" printed on the screen.

what am I doing wrong?

bg,
Johannes

-- 
GLOBE Development GmbH
Königsberger Strasse 260
48157 MünsterGLOBE Development GmbH
Königsberger Strasse 260
48157 Münster
0251/5205 390




More information about the erlang-questions mailing list