-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.bonus.c
91 lines (82 loc) · 2.07 KB
/
client.bonus.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
90
91
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Asandy <abasifrekesandy@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/14 17:32:14 by Asandy #+# #+# */
/* Updated: 2022/08/14 17:32:14 by Asandy ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include "libft/libft.h"
int g_len;
int checkstr(char *str)
{
int i;
i = 0;
while (str[i])
{
if (isdigit(str[i]))
i++;
else
return (0);
}
return (1);
}
void acknow(int sig)
{
if (sig == SIGUSR1)
ft_printf("Server received %d characters", g_len);
exit (1);
}
void send_data(int pid, char c)
{
int i;
int j;
i = 7;
j = 0;
while (i >= 0)
{
if ((c >> i & 1) == 1)
j = kill(pid, SIGUSR1);
else
j = kill(pid, SIGUSR2);
usleep(150);
i--;
}
if (j == -1)
{
ft_putstr_fd("Kill function failed to connect. Check PID.\n", 2);
exit (1);
}
}
int main(int argc, char **argv)
{
int pid;
int i;
i = 0;
if (argc == 3)
{
if ((checkstr(argv[1])) != 1)
ft_putstr_fd("Error: Invalid PID Format\n", 2);
pid = ft_atoi(argv[1]);
while (argv[2][i])
{
send_data(pid, argv[2][i]);
i++;
}
send_data(pid, '\0');
g_len = ft_strlen(argv[2]);
signal(SIGUSR1, acknow);
while (1)
pause();
}
else
ft_putendl_fd("Wrong number of arguments : ./client 'pid' 'text'", 2);
return (0);