-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
88 lines (80 loc) · 1.96 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ogenc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/26 00:08:43 by ogenc #+# #+# */
/* Updated: 2023/03/07 20:54:01 by ogenc ### ########.fr */
/* */
/* ************************************************************************** */
#include"minitalk.h"
void send_binary_signal(char *binary, int sp_id)
{
int i;
i = 0;
while (binary[i])
{
if (binary[i] == '0')
kill(sp_id, SIGUSR2);
else if (binary[i] == '1')
kill(sp_id, SIGUSR1);
usleep(5000);
i++;
}
}
void send_client_pid(int server_pid)
{
char *cpid;
int i;
char *c_binary;
int x;
i = 0;
cpid = itoa_pid(getpid());
while (cpid[i])
{
c_binary = convertbinary(cpid[i]);
x = 0;
while (c_binary[x])
{
if (c_binary[x] == '0')
kill(server_pid, SIGUSR2);
else if (c_binary[x] == '1')
kill(server_pid, SIGUSR1);
usleep(1000);
x++;
}
free(c_binary);
i++;
}
free(cpid);
}
void message(int signum)
{
if (signum == SIGUSR1)
write(1, "SUCCESS!\n", 10);
}
int main(int argc, char **argv)
{
int i;
int len;
char *binary;
i = 0;
signal(SIGUSR1, message);
if (argc == 3)
{
send_client_pid(ft_atoi(argv[1]));
len = ft_strlen(argv[2]);
while (len >= i)
{
binary = convertbinary(argv[2][i]);
send_binary_signal(binary, ft_atoi(argv[1]));
free(binary);
i++;
}
}
else
write(1 ,"ERROR", 6);
return (0);
}