-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
88 lines (76 loc) · 2.25 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchow-so <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/10 10:34:19 by tchow-so #+# #+# */
/* Updated: 2024/11/10 10:35:35 by tchow-so ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
static void check_error(int argc, char **argv);
static void send_msg(pid_t pid, const char *msg);
static void send_len(pid_t pid, unsigned int len);
static void send_char(pid_t pid, unsigned char c);
int main(int argc, char **argv)
{
pid_t server_pid;
check_error(argc, argv);
server_pid = ft_atoi(argv[1]);
send_msg(server_pid, argv[2]);
return (EXIT_SUCCESS);
}
static void check_error(int argc, char **argv)
{
pid_t server_pid;
int i;
if (argc != 3)
printerr_exit("Usage: ./client <server_PID> <message>\n");
i = 0;
while (argv[1][i] != '\0')
{
if (!ft_isdigit(argv[1][i]))
printerr_exit("Invalid PID\n");
i++;
}
server_pid = ft_atoi(argv[1]);
if (kill(server_pid, 0) == -1)
printerr_exit("Process does not exist\n");
}
static void send_msg(pid_t pid, const char *msg)
{
unsigned int msg_len;
msg_len = ft_strlen(msg);
send_len(pid, msg_len);
while (*msg)
send_char(pid, *msg++);
send_char(pid, '\0');
}
static void send_len(pid_t pid, unsigned int len)
{
int bit;
int bitshift;
bit = 0;
bitshift = (sizeof(int) * 8) - 1;
while (bitshift >= 0)
{
bit = (len >> bitshift) & 1;
send_bit(pid, bit);
bitshift--;
}
}
static void send_char(pid_t pid, unsigned char c)
{
int bit;
int bitshift;
bit = 0;
bitshift = (sizeof(char) * 8) - 1;
while (bitshift >= 0)
{
bit = (c >> bitshift) & 1;
send_bit(pid, bit);
bitshift--;
}
}