Skip to content

Instantly share code, notes, and snippets.

@DexterHaslem
Created January 2, 2015 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DexterHaslem/bb6349dda8ed4f409bd8 to your computer and use it in GitHub Desktop.
Save DexterHaslem/bb6349dda8ed4f409bd8 to your computer and use it in GitHub Desktop.
-module(fix).
-export([start_link/0, loop0/1, worker/2]).
-define(PORTNO, 2015).
start_link() ->
start_link(?PORTNO).
start_link(P) ->
spawn_link(?MODULE, loop0, [P]).
loop0(Port) ->
case gen_tcp:listen(Port, [binary, {reuseaddr, true},
{packet, 0}, {active, false}]) of
{ok, LSock} ->
spawn(?MODULE, worker, [self(), LSock]),
loop(LSock);
Other ->
io:format("Can't listen to socket ~p~n", [Other])
end.
loop(S) ->
receive
next_worker ->
spawn_link(?MODULE, worker, [self(), S])
end,
loop(S).
worker(Server, LS) ->
case gen_tcp:accept(LS) of
{ok, Socket} ->
Server ! next_worker,
send_data(Socket);
{error, Reason} ->
Server ! next_worker,
io:format("Can't accept socket ~p~n", [Reason])
end.
send_data(Socket) ->
Data = "Data",
case gen_tcp:send(Socket, Data) of
{error, _Msg} -> exit(normal);
ok -> exit(normal)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment