forked from zeliard/RIOTcpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestclient.erl
28 lines (25 loc) · 841 Bytes
/
testclient.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-module(testclient).
-export([start_client/0]).
-define(TCP_OPTIONS, [binary, {packet, 0}, {active, true}]).
-define(PORT, 9001).
-define(HOST, "127.0.0.1").
-define(DATASIZE, 4096).
loop(Socket) ->
receive
{tcp, Socket, Bin} ->
gen_tcp:send(Socket, Bin),
loop(Socket);
{tcp_closed, Socket}->
io:format("Socket ~p closed~n", [Socket]);
{tcp_error, Socket, Reason} ->
io:format("Error on socket ~p reason: ~p~n", [Socket, Reason])
end.
start_client() ->
{ok, Sock} = gen_tcp:connect(?HOST, ?PORT, ?TCP_OPTIONS),
Pid = spawn(fun() ->
io:format("Connection established ~n", []),
loop(Sock)
end),
gen_tcp:controlling_process(Sock, Pid),
Message = crypto:strong_rand_bytes(?DATASIZE),
gen_tcp:send(Sock, Message).