Last active
March 31, 2018 14:37
-
-
Save kauffmanes/f4c7acee0f49dbfaba831a6c3451e5a0 to your computer and use it in GitHub Desktop.
The Client application for my Erlang chat server
This file contains hidden or 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
%% @author kauff | |
%% @doc @todo Add description to client1. | |
-module(client1). | |
%% ==================================================================== | |
%% API functions | |
%% ==================================================================== | |
-export([connect/0, connect/1, message/2]). | |
-define(TCP_OPTIONS, [binary, {packet, 0}, {active, false}, {reuseaddr, true}]). | |
connect () -> connect(8080). | |
connect (Port) -> | |
{ok, Socket} = gen_tcp:connect("localhost", Port, ?TCP_OPTIONS), | |
io:format("Joining...~n"), | |
spawn(fun() -> recv(Socket) end). | |
message(Socket, Msg) -> | |
Bin = term_to_binary(Msg), | |
gen_tcp:send(Socket, Bin). | |
recv(Socket) -> | |
case gen_tcp:recv(Socket, 0) of | |
{ok, Answer} -> | |
io:format("Received: ~p~n", [Answer]), | |
recv(Socket); | |
Other -> | |
io:format("Error:~w~n", [Other]) | |
end. | |
%% ==================================================================== | |
%% Internal functions | |
%% ==================================================================== | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment