-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
89 lines (81 loc) · 2.09 KB
/
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
88
89
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adantas- <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/19 21:06:11 by adantas- #+# #+# */
/* Updated: 2023/01/23 16:18:19 by adantas- ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
#include "libft/libft.h"
#include "libft/ft_printf.h"
int g_bit;
void received(int sig)
{
if (sig == SIGUSR1)
g_bit++;
else if (sig == SIGUSR2)
g_bit++;
else
{
ft_printf("❗\tError: Signal is invalid.\n");
exit(22);
}
}
void validation(int ac, char **av)
{
size_t i;
int pid;
if (ac != 3)
{
ft_printf("⚠️\tError: Wrong number of arguments.\n");
ft_printf("❗\tUsage: ./client <PID> <string>\n");
exit(22);
}
i = -1;
pid = ft_atoi(av[1]);
while (av[1][++i])
{
if (!ft_isdigit(av[1][i]) || pid <= 0)
{
ft_printf("⚠️\tError: PID is invalid.\n");
ft_printf("🔢\tA PID only has positive decimal numbers. ");
ft_printf("PID 0 is reserved for the kernel.\n");
exit(22);
}
}
}
void send_bit(int srv_pid, char c)
{
while (g_bit < 8)
{
if ((128 >> g_bit) & c)
kill(srv_pid, SIGUSR1);
else
kill(srv_pid, SIGUSR2);
usleep(5000);
if (g_bit == 8)
{
g_bit = 0;
return ;
}
}
}
int main(int ac, char **av)
{
size_t i;
int srv_pid;
signal(SIGUSR1, received);
signal(SIGUSR2, received);
validation(ac, av);
srv_pid = ft_atoi(av[1]);
i = -1;
g_bit = 0;
while (av[2][++i])
send_bit(srv_pid, av[2][i]);
send_bit(srv_pid, '\n');
exit(0);
}