[erlang-questions] Simple TCP Server
Ari King
ari.brandeis.king@REDACTED
Wed Dec 18 18:40:04 CET 2013
I incorporated the suggestions and it solved the issue of setting up a
socket to listen on a specified port(line 5). However, a new issue has
risen while attempting to accept a connection on the listening socket. The
error message follows:
=ERROR REPORT==== 18-Dec-2013::17:32:47 ===
Error in process <0.32.0> with exit value:
{{badmatch,{error,closed}},[{mock_tcp,connect,2,[{file,"mock_tcp.erl"},{line,12}]}]}
I'm starting the server via erl -s mock_tcp start 8080 <path_to_file>
-module(mock_tcp).
-export([start/1]).
start([AtomPort, AtomFilename]) ->
Port = list_to_integer(atom_to_list(AtomPort)),
Filename = atom_to_list(AtomFilename),
{ok, Listen} = gen_tcp:listen(Port, [binary, {reuseaddr, true}]),
spawn(fun() -> connect(Listen, Filename) end),
io:format("Server available on port ~p. Reading from file ~p.", [Port,
Filename]).
connect(Listen, Filename) ->
% Following line causes the error
{ok, Socket} = gen_tcp:accept(Listen),
spawn(fun() -> connect(Listen, Filename) end),
{ok, Device} = file:open(Filename, read),
get_data(Device, Socket).
get_data(Device, Socket) ->
case io:get_line(Device) of
{ok, Data} ->
gen_tcp:send(Socket, Data),
get_data(Device, Socket);
eof ->
file:close(Device),
gen_tcp:close(Socket)
end.
Thanks.
-Ari
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131218/2991d4a7/attachment.htm>
More information about the erlang-questions
mailing list