Created
February 2, 2013 23:58
-
-
Save dsociative/4699827 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
%% Copyright | |
-module(serv). | |
-author("dsociative"). | |
%% API | |
-export([listen/1, listen/0, talk/1]). | |
listen() -> | |
listen(9998). | |
listen(Port) -> | |
case gen_tcp:listen(Port, [binary, {packet, 0}, {active, false}]) of | |
{ok, ServerSocket} -> | |
wait_client(ServerSocket); | |
{error, Reason} -> | |
io:format('Listen start error: ~s \n', [Reason]) | |
end. | |
wait_client(ServerSocket) -> | |
spawn_talk(gen_tcp:accept(ServerSocket)), | |
wait_client(ServerSocket). | |
spawn_talk({ok, Client}) -> | |
spawn(?MODULE, talk, [Client]); | |
spawn_talk({error, Reason}) -> | |
Reason. | |
talk(Client) -> | |
case listen_msg(Client) of | |
{ok, Message} -> | |
process_msg(Message), | |
talk(Client); | |
{error, Reason} -> | |
gen_tcp:close(Client) | |
end. | |
listen_msg(Client) -> | |
gen_tcp:recv(Client, 0). | |
process_msg(Message) -> | |
io:format("~ts~n", [Message]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment