I decided to modify the code in this thread by changing the server to a parallel one (as mentioned in the book) and it doesn't work properly. The following code is supposed to be presto-chango according to the book "Programming Erlang" but I get an error on line 17. Here is the parallel server code (the completed parallel server is on the bottom, this is just the change to the old server code on the last thread),
startp() ->
{ok, Listen} = gen_tcp:listen(4042, [binary, {packet, 0},
{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).
and this is the error,
=ERROR REPORT==== 12-Feb-2013::23:05:13 ===
Error in process <0.35.0> with exit value: {{badmatch,{error,einval}},[{ps1,loop,1,[{file,"ps1.erl"},{line,17}]}]}
The following is the parallel server code, I'm thinking that something is wrong with the argument on line 14 for the call loop(Socket). Maybe its something else, I dunno?
-module(ps1).
-compile(export_all).
-import(lists, [reverse/1]).
startp() ->
{ok, Listen} = gen_tcp:listen(4042, [binary, {packet, 0},
{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(Listen) ->
{ok, Socket} = gen_tcp:accept(Listen),
receive
{tcp, Socket, Bin} ->
io:format("Server received binary = ~p~n",[Bin]),
Str = binary_to_term(Bin),
io:format("Server (unpacked) ~p~n",[Str]),
Reply = string:to_upper(Str),
io:format("Server replying = ~p~n",[Reply]),
gen_tcp:send(Socket, term_to_binary(Reply)),
loop(Listen);
{tcp_closed, Socket} ->
io:format("Server socket closed~n")
end.
next is the client code which also ends up dieing waiting for a reply
c1:nano("list_to_tuple([2+3*4,10+20])").
nano got not answer to "list_to_tuple([2+3*4,10+20])"
{error,timeout}
-module(c1).
-compile(export_all).
-import(lists, [reverse/1]).
%old port 2345
nano(Str) ->
{ok, Socket} =
gen_tcp:connect("localhost", 4042,
[binary, {packet, 0}]),
ok = gen_tcp:send(Socket, term_to_binary(Str)),
R = receive
{tcp,Socket,Bin} ->
io:format("Client received binary = ~p~n",[Bin]),
Val = binary_to_term(Bin),
io:format("Client result = ~p~n",[Val]),
{ok,Val}
after 5000 ->
io:format("nano got no answer to ~p~n",[Str]),
{error,timeout}
end,
% gen_tcp:close(Socket),
R.