-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
90 lines (81 loc) · 2.12 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bpisak-l <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/01 15:31:23 by bpisak-l #+# #+# */
/* Updated: 2024/05/08 13:13:45 by bpisak-l ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
unsigned int g_server_exists;
void validate_pid(pid_t pid, const char *arg)
{
if (ft_strncmp(ft_itoa(pid), arg, ft_strlen(arg)) || pid <= 0)
{
write(2, "Invalid server pid.\n", 20);
exit (1);
}
}
void send_char(char c, pid_t server_pid)
{
char bit;
char i;
i = 8;
while (i--)
{
bit = c & 0b10000000;
pause();
if (bit)
kill(server_pid, SIGUSR2);
else
kill(server_pid, SIGUSR1);
c = c << 1;
}
}
void handler(int signum)
{
if (signum == SIGUSR2)
ft_printf("(Seen)\n");
else
{
g_server_exists = 1;
usleep(40);
}
}
void finalize(pid_t server_pid)
{
send_char(0, server_pid);
signal(SIGUSR1, SIG_IGN);
signal(SIGUSR2, handler);
pause();
}
int main(int argc, char const *argv[])
{
pid_t server_pid;
int i;
g_server_exists = 0;
if (argc != 3)
{
write(2, "Correct usage: ./client <server_pid> <message_string>\n", 54);
return (1);
}
signal(SIGUSR1, handler);
server_pid = (pid_t)ft_atoi(argv[1]);
validate_pid(server_pid, argv[1]);
kill(server_pid, SIGUSR1);
usleep(100);
if (!g_server_exists)
{
write(2, "Error: Server is not available.\n", 30);
return (1);
}
kill(server_pid, SIGUSR1);
i = -1;
while (argv[2][++i])
send_char(argv[2][i], server_pid);
finalize(server_pid);
return (0);
}