no data received on server side

In a nutshell: I am trying to make a socket server to which clients connect and send/receive messages (based on the sockserv code in Learn you some erlang tutorial http://learnyousomeerlang.com/buckets-of-sockets) Server side components: supervisor - unique, started at the very beginning, spawns processes with gen_server behaviour gen_server behaviour processes - each one deals with a connection.

Client side: client which connects to the socket and sends a few bytes of data and then disconnects.

Code details My code is pretty much the same as in the presented tutorial. The supervisor is identical. The gen_server component is simplified so that it has only one handle_info case which is supposed to catch everything and just print it.

Problem The connection succeeds, but when the client sends data, the server behaves as though no data is received (I am expecting that handle_info is called when that happens).

handle_info does get called but only when the client disconnects and this event is reported with a message.

My attempts I have played around with different clients written in Erlang or Java, I have tried setting the active/passive state of the socket. The author of the tutorial sets {active, once} after sending a message. I ended up just setting {active, true} after the AcceptSocket is created as such: (the gen_server proc is initialized with a state which contains the original ListenSocket created by the supervisor)

handle_cast(accept, S = #state{socket=ListenSocket}) ->

     {ok, AcceptSocket} = gen_tcp:accept(ListenSocket),
     io:format("accepted connection ~n", []),

      sockserv_sup:start_socket(), % a new acceptor is born, praise the lord
      inet:setopts(AcceptSocket, [{active, true}]),
      send(AcceptSocket, "Yellow", []),
      {noreply, S#state{socket=AcceptSocket, next=name}}.

send(Socket, Str, Args) ->
    ok = gen_tcp:send(Socket, io_lib:format(Str++"~n", Args)),
    ok.

   handle_info(E, S) ->
       io:format("mothereffing unexpected: ~p~n", [E]),
       {noreply, S}.

It has aboslutely no effect. handle_info only gets called when the connection is lost because the client disconnects. whenever the client sends data nothing happens.

What could be the problem? I have spend quite some time on this, I really have no idea. Many thanks.


Have you tried setting the other options in http://www.erlang.org/doc/man/inet.html#setopts-2

inet:setopts(AcceptSocket, [{active, true}])

for example:

{packet, line} to read in a line at a time

and

binary to read in data as a binary.

I also was working through a similar exercise based on that tutorial recently and my options used were:

inet:setopts(LSocket, [{active,true}, {packet, line}, binary, {reuseaddr, true}]),


To conclude, watch out for the options. I was indeed not paying attention to the implications of the set of options. I tried with a more narrowed down situation and worked it out. My problem was the {packet, line} option which implies that n is considered a message delimiter.

链接地址: http://www.djcxy.com/p/38234.html

上一篇: Erlang用一个参数产生一个主管

下一篇: 在服务器端没有收到数据