Created
March 31, 2018 14:36
-
-
Save kauffmanes/64c5b3608979f34de1cc0196d55261c0 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
%% @author kauff | |
%% @doc @todo Add description to client1. | |
-module(client1). | |
%% ==================================================================== | |
%% API functions | |
%% ==================================================================== | |
-export([connect/0, connect/1, send/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). | |
send(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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment