-
Notifications
You must be signed in to change notification settings - Fork 48
/
req_client.erl
45 lines (41 loc) · 1.65 KB
/
req_client.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at http://mozilla.org/MPL/2.0/.
-module(req_client).
-export([main/0]).
main() ->
application:start(chumak),
{ok, Socket} = chumak:socket(req, "my-req"),
case chumak:connect(Socket, tcp, "localhost", 5555) of
{ok, Pid} ->
send_messages(Socket, [
<<"Hello my dear friend">>,
<<"Hello my old friend">>,
<<"Hello all the things">>
]);
{error, Reason} ->
io:format("Connection Failed for this reason: ~p\n", [Reason]);
Reply ->
io:format("Unhandled reply for connect ~p \n", [Reply])
end.
send_messages(Socket, []) ->
send_messages(Socket, [
<<"Hello my dear friend">>,
<<"Hello my old friend">>,
<<"Hello all the things">>
]);
send_messages(Socket, [Message|Messages]) ->
case chumak:send(Socket, Message) of
ok ->
io:format("Send message: ~p\n", [Message]);
{error, Reason} ->
io:format("Failed to send message: ~p, reason: ~p\n", [Message, Reason])
end,
case chumak:recv(Socket) of
{ok, RecvMessage} ->
io:format("Recv message: ~p\n", [RecvMessage]);
{error, RecvReason} ->
io:format("Failed to recv, reason: ~p\n", [RecvReason])
end,
timer:sleep(1000),
send_messages(Socket, Messages).