-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_bonus.c
111 lines (101 loc) · 2.17 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gemartin <[email protected]...> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/04 12:05:57 by gemartin #+# #+# */
/* Updated: 2022/03/10 14:50:08 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include<signal.h>
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<limits.h>
#include<stdlib.h>
int ft_atoi(const char *str)
{
int i;
long long int nbr;
nbr = 0;
i = 0;
while (str[i] >= '0' && str[i] <= '9')
{
nbr = nbr * 10 + str[i] - '0';
i++;
if (nbr > INT_MAX)
return (-1);
}
return (nbr);
}
void conv_int_bin(unsigned int c, int pidserv)
{
unsigned int base;
unsigned int cont;
cont = 0;
base = 2147483648;
if (cont < 32)
{
while (base > 0)
{
if (c >= base)
{
kill(pidserv, SIGUSR1);
c = c - base;
}
else
{
kill(pidserv, SIGUSR2);
}
base = base / 2;
usleep(300);
}
}
usleep(500);
cont++;
}
void conv_bin(unsigned char c, int pidserv)
{
int base;
base = 128;
while (base > 0)
{
if (c >= base)
{
kill(pidserv, SIGUSR1);
c = c - base;
}
else
{
kill(pidserv, SIGUSR2);
}
base = base / 2;
usleep(300);
}
usleep(500);
}
void confirm(int sig)
{
if (sig == SIGUSR1)
write (1, "Received bit\n", 13);
}
int main(int argc, char **argv)
{
int pidserv;
int i;
i = 0;
if (argc != 3)
return (-1);
signal(SIGUSR1, confirm);
pidserv = ft_atoi(argv[1]);
conv_int_bin(getpid(), pidserv);
while (argv[2][i])
{
conv_bin(argv[2][i], pidserv);
i++;
}
conv_bin('\0', pidserv);
return (0);
}