Created
January 2, 2015 16:41
-
-
Save DexterHaslem/bb6349dda8ed4f409bd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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