-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_client.c
87 lines (77 loc) · 2.05 KB
/
ft_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ael-khni <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/09 20:38:27 by ael-khni #+# #+# */
/* Updated: 2021/12/15 19:42:56 by ael-khni ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_minitalk.h"
void signal_error(void)
{
ft_printf("\n%sclient: unexpected error.%s\n", RED, END);
exit(EXIT_FAILURE);
}
void char_to_bin(unsigned char c, int pid)
{
int bit;
bit = 0;
while (bit < 8)
{
if (c & 128)
{
if (kill(pid, SIGUSR2) == -1)
signal_error();
}
else
{
if (kill(pid, SIGUSR1) == -1)
signal_error();
}
c <<= 1;
bit++;
pause();
usleep(100);
}
}
void sent_text(char *str, int pid)
{
int i;
i = 0;
while (str[i])
char_to_bin(str[i++], pid);
char_to_bin('\0', pid);
}
void recieved(int sig)
{
static int sent;
if (sig == SIGUSR1)
{
ft_printf("%s%d signal sent successfully!%s\n", GREEN, ++sent, END);
exit(EXIT_SUCCESS);
}
if (sig == SIGUSR2)
++sent;
}
int main(int ac, char **av)
{
int server_pid;
int client_pid;
client_pid = getpid();
if (ac == 3)
{
ft_printf("%sclient pid: %d%s\n", RED, client_pid, END);
signal(SIGUSR1, recieved);
signal(SIGUSR2, recieved);
server_pid = ft_atoi(av[1]);
ft_printf("%sText currently sending.. %s\n", YELLOW, END);
sent_text(av[2], server_pid);
}
else
ft_printf("%susage: ./client <server_pid> <text to send>%s\n",
RED, END);
return (EXIT_FAILURE);
}