-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
118 lines (107 loc) · 2.55 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mkaruvan <mkaruvan@student.42abudhabi.a +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/10 10:13:30 by mkaruvan #+# #+# */
/* Updated: 2023/06/27 09:39:08 by mkaruvan ### ########.fr */
/* */
/* ************************************************************************** */
#include <sys/types.h>
#include <signal.h>
#include <stdbool.h>
#include "libft/includes/libft.h"
#include "libft/includes/ft_printf.h"
bool g_flag = false;
void ft_err(void)
{
ft_printf("Invalid Server PID!!!");
exit (1);
}
void sender(int server_pid, int sig)
{
int j;
j = 0;
while (1)
{
usleep(2);
j = kill(server_pid, sig);
if (j == 0)
break ;
else if (j == -1)
ft_err();
}
}
void handler(int sig, siginfo_t *siginfo, void *context)
{
static int recd_bit;
static int recd_char;
(void) siginfo;
(void) context;
if (sig == SIGUSR1)
{
recd_bit++;
g_flag = false;
if (recd_bit == 8)
{
recd_char++;
recd_bit = 0;
}
}
else
{
ft_printf("Number of Character's recd = %d\n", recd_char - 1);
exit (0);
}
}
void send_data(unsigned char character, int pid)
{
int index;
int count;
index = 7;
count = 0;
while (index >= 0)
{
sender(pid, SIGUSR2 - (character >> index-- & 1));
g_flag = true;
while (g_flag)
{
usleep(2);
count++;
if (count > 10000)
{
count = 0;
ft_printf("*");
g_flag = false;
}
}
}
}
int main(int argc, char **argv)
{
int pid;
int index;
struct sigaction sa;
index = 0;
sa.sa_sigaction = handler;
sa.sa_flags = SA_SIGINFO;
if (argc == 3)
{
pid = ft_matoi(argv[1]);
if (pid <= 0)
ft_err();
if (sigaction(SIGUSR1, &sa, NULL) == -1 \
|| sigaction(SIGUSR2, &sa, NULL) == -1)
return (ft_printf("Error registering SIGUSR signal handler.\n"));
ft_printf("Number of character's send = %d\n", ft_strlen(argv[2]));
while (argv[2][index])
send_data(argv[2][index++], pid);
send_data('\0', pid);
usleep(500);
}
else
ft_putendl_fd("Wrong Number of arguments", 2);
return (1);
}