-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample-client.c
48 lines (40 loc) · 984 Bytes
/
example-client.c
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
46
47
48
#include <stdio.h>
#include "sscilib.h"
/* report every msg received from server */
void
on_messg(Conn *cli, const char *msg, unsigned int len)
{
printf("Message from server: %s\n", msg);
}
/* Send text from std input */
void
on_stdin(Conn *ctx, const char *msg, unsigned int len)
{
client_send(ctx, msg, len);
}
/* Report status on closed connexion */
void
status(Conn *cli, const char *msg, unsigned int len)
{
printf("Check if client is alive: %d\n", client_alive(cli));
}
int
main(void)
{
/* Dial server at 127.0.0.1:1337 */
Conn *cli = client_dial("127.0.0.1", 1337);
if (!cli)
return 1;
/* Bind callback functions to events */
client_bind(cli, ON_MESSG, on_messg);
client_bind(cli, ON_STDIN, on_stdin);
client_bind(cli, ON_CLOSE, status);
status(cli, NULL, 0);
/* Greet the server and wait for response */
client_send(cli, "Hello", 5);
client_poll(cli, -1);
/* Wait 5s for an event and close */
client_poll(cli, 5000);
client_close(cli);
return 0;
}