-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
71 lines (66 loc) · 1.97 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgaudin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/09 15:02:14 by lgaudin #+# #+# */
/* Updated: 2023/06/10 14:29:08 by lgaudin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include <signal.h>
#include <stdio.h>
#include <string.h>
/**
* @brief Sends 8 signals to the provided PID to transmit the
* provided character bit by bit.
* It starts from the MSB and progressively goes to the LSB.
*
* It sends SIGUSR1 if the bit is 1, SIGUSR2 if it is 0.
*
* @param pid server's PID
* @param character character to transmit
*/
void send_signal(int pid, unsigned char character)
{
int i;
unsigned char temp_char;
i = 8;
temp_char = character;
while (i > 0)
{
i--;
temp_char = character >> i;
if (temp_char % 2 == 0)
kill(pid, SIGUSR2);
else
kill(pid, SIGUSR1);
usleep(42);
}
}
/**
* @brief Sends a message to the server character by character.
*
* @param argc
* @param argv
*/
int main(int argc, char *argv[])
{
pid_t server_pid;
const char *message;
int i;
if (argc != 3)
{
ft_printf("Usage: %s <pid> <message>\n", argv[0]);
exit(0);
}
server_pid = ft_atoi(argv[1]);
message = argv[2];
i = 0;
while (message[i])
send_signal(server_pid, message[i++]);
send_signal(server_pid, '\0');
return (0);
}