-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
96 lines (86 loc) · 2.24 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
90
91
92
93
94
95
96
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tanas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/01 14:43:39 by tanas #+# #+# */
/* Updated: 2023/06/15 16:06:01 by tanas ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
int g_strlen;
static pid_t check_pid(char *pid_c)
{
int i;
pid_t pid;
i = -1;
while (pid_c[++i])
{
if (! ((pid_c[i] >= '0') && (pid_c[i] <= '9')))
{
ft_printf(BI_RED"\nError! Invalid PID entered.\n\n"RESET);
exit(1);
}
}
pid = ft_atoi(pid_c);
if (pid <= 0 || pid >= 100000)
{
ft_printf(BI_RED"\nError! Invalid PID entered.\n\n"RESET);
exit(2);
}
return (pid);
}
static void sigusr1_handler(int signum)
{
static int char_count;
if (signum == SIGUSR1)
char_count++;
if (char_count == g_strlen)
{
ft_printf(B_GREEN \
"Message sent successfully!\nTotal Characters Received: %i\n" \
RESET, char_count);
exit(3);
}
}
static void send_signal(int pid, char *message)
{
int i;
int bitshift;
i = -1;
while (message[++i])
{
bitshift = 8;
while (--bitshift >= 0)
{
if (message[i] & (1 << bitshift))
kill(pid, SIGUSR1);
else
kill(pid, SIGUSR2);
usleep (100);
}
}
}
// client
int main(int argc, char **argv)
{
pid_t pid;
struct sigaction act;
if (argc != 3)
{
ft_printf(BI_RED"\nError! Invalid number of arguments entered.\n\n"RESET);
exit(0);
}
if (!argv[2][0])
{
ft_printf(BI_RED"\nError! Empty string entered.\n\n"RESET);
exit(0);
}
pid = check_pid(argv[1]);
g_strlen = ft_strlen(argv[2]);
act.sa_handler = sigusr1_handler;
sigaction(SIGUSR1, &act, NULL);
send_signal(pid, argv[2]);
}