-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
79 lines (72 loc) · 1.84 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: stigkas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/18 10:15:01 by stigkas #+# #+# */
/* Updated: 2024/01/19 16:12:45 by stigkas ### ########.fr */
/* */
/* ************************************************************************** */
//Sending a message to the server!!
#include "includes/minitalk.h"
int ft_send_str_len(int server_pid, char *str)
{
int i;
size_t str_len;
i = 0;
str_len = ft_strlen(str);
if (str_len >= MAX_LEN)
{
ft_printf("The message is too long");
return (0);
}
while (i < 32)
{
kill(server_pid, SIGUSR1 + (1 & (str_len >> i)));
usleep(60);
i++;
}
return (1);
}
void ft_send_str(int server_pid, char *str)
{
int i;
int c;
while (*str)
{
c = *str;
i = 0;
while (i < 8)
{
kill(server_pid, SIGUSR1 + (1 & (c >> i)));
usleep(75);
i++;
}
str++;
}
}
int main(int argc, char **av)
{
int server_pid;
if (argc == 3)
{
server_pid = ft_atoi(av[1]);
if (server_pid <= 0)
{
ft_putstr_fd("Invalid PID: ", 2);
ft_putstr_fd(av[1], 2);
exit(1);
}
if (!ft_send_str_len(server_pid, av[2]))
exit(0);
ft_send_str(server_pid, av[2]);
}
else
{
ft_printf("To talk with the client: ./client [PID] [string]\n");
exit(1);
}
return (0);
}