[erlang-questions] Simple TCP Server

Dmitry Demeshchuk <>
Tue Dec 17 20:59:07 CET 2013


You might want to add a {reuseaddr, true} option to the listening socket
options.

If this doesn't help, try actually logging the result of gen_tcp:listen/2
before matching it, this would be much faster to figure out.


On Tue, Dec 17, 2013 at 10:53 AM, Ari King <>wrote:

> Hi,
>
> The code below results in "init terminating in do_boot." Seemingly the
> problem is on line 5, does anyone see what is wrong with it? Additionally,
> I'm trying to pass the port in as a command line argument, but for some
> reason it doesn't work and I need to hardcode it.
>
> -module(mock_tcp).
> -export([start/1]).
>
> start([Port, Filename]) ->
>   {ok, Listen} = gen_tcp:listen(Port, [binary]),
>   spawn(fun() -> connect(Listen, Filename) end),
>   io:format("Server available on port ~w. Reading from file ~w.", [Port,
> Filename]).
>
> connect(Listen, Filename) ->
>   {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
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>


-- 
Best regards,
Dmitry Demeshchuk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131217/3f136349/attachment.html>


More information about the erlang-questions mailing list