gen tcp - Erlang missing messages -


i'm running following code dbg:p(client, r):

-module(client). -export([start/0, start/2, send/1, net_client/1]).  start() ->     start("localhost", 7000).  start(host, port) ->     io:format("client connecting ~p:~p.~n", [host, port]),     register(ui, spawn(fun() -> gui_control([]) end)),     case gen_tcp:connect(host, port, [binary, {packet, 0}]) of         {ok, socket} ->             pid = spawn(client, net_client, [socket]),             register(client, pid),             gen_tcp:controlling_process(socket, pid);         error ->             io:format("error connecting server: ~p~n", [error]),             erlang:error("could not connect server.")     end,     ok.  send(msg) ->     client!{send, msg}.   %% forwards messages either gui controller or server. net_client(socket) ->     receive         {tcp, socket, message} ->             msg = binary_to_term(message),             io:format("received tcp message on ~p: ~p~n", [socket, msg]),             ui!{server, msg};         {send, message} ->             io:format("sending ~p.~n", [message]),             gen_tcp:send(socket, term_to_binary(message));         close ->             gen_tcp:close(socket),             exit(normal);         {tcp_closed, socket} ->             io:format("server terminated connection.~n"),             exit(normal); %% reconnect?         timeout -> %%             io:format("timed out?~n");         {inet_reply, socket, message} -> %% , never happen.             io:format("inet_reply: ~p~n", message);         error ->             io:format("net client got bad message: ~p.~n", [error])     after 10000 ->             refresh %% gen_tcp:send(socket, term_to_binary(keepalive))     end,     ?module:net_client(socket).   gui_control(data) ->     receive         %% hang gui until sync done. not sure if         %% that's okay.         {server, {sync, datum}} -> % resync command server.             gui_control(resync([datum]));         {client, refresh} -> % refresh request display.             display:update(data);         {server, msg} ->             io:format("ui rx: ~p~n", [msg])     end,     gui_control(data).  resync(data) ->     receive         {server, {sync, datum}} ->             resync([datum|data]);         {server, {done, num}} ->             case length(data) of                 num ->                     data;                 _ ->                     io:format("got done before data received.~n"),                     send({sync})             end     after 5000 ->             io:format("timed out waiting data.~n"),             send({sync})     end. 

it communicates server wrote gen_tcp , gen_server, following this. main problem don't reliably receive messages. i'll get

(<0.2.0>) << {tcp,#port<0.517>,                   <<131,104,6,100,0,4,99,97,114,100,100,0,7,117,110,107,110,                     111,119,110,100,0,7,117,110,107,110,111,119,110,106,106,                     104,3,107,0,6,83,101,99,111,110,100,100,0,4,100,114,97,                     119,97,2,131,104,6,100,0,4,99,97,114,100,100,0,7,117,110,                     107,110,111,119,110,100,0,7,117,110,107,110,111,119,110,                     106,106,104,3,107,0,6,83,101,99,111,110,100,100,0,4,100,                     114,97,119,97,3,131,104,6,100,0,4,99,97,114,100,100,0,7,                     117,110,107,110,111,119,110,100,0,7,117,110,107,110,111,                     119,110,106,106,104,3,107,0,5,70,105,114,115,116,100,0,4,                     100,114,97,119,97,0>>} 

from debugging output, no corresponding received tcp message on #port<0.517>:... message. i'll see things this:

(<0.2.0>) << {io_reply,<0.24.0>,ok} (<0.2.0>) << timeout (<0.2.0>) << {io_reply,<0.24.0>,ok} (<0.2.0>) << timeout (<0.2.0>) << {io_reply,<0.24.0>,ok} 

but nothing net_client's receive. i've watched network traffic wireshark , know packets getting they're supposed go , being acked. doing wrong?

edit: i'm invoking erl -smp enable -eval "client:start()." in case matters.

i guess basic problem 'net_client' should spawned off separate process..

in start method, change

register(client, self()), net_client(socket); 

to

register(client, fun() -> net_client(socket) end); 

that should solve it..


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -