-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
70 lines (63 loc) · 2.26 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* client.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: passunca <passunca@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/18 12:04:30 by passunca #+# #+# */
/* Updated: 2024/02/22 17:47:00 by passunca ### ########.fr */
/* */
/* ************************************************************************** */
#include "minitalk.h"
static void ft_client_sighandler(int sig);
static void ft_send_msg(pid_t pid, char *msg);
int main(int argc, char **argv)
{
struct sigaction sa;
if (argc != 3)
ft_perror_exit("Usage: ./client [PID] [message]\n");
else if (kill(ft_atoi(argv[1]), 0) < 0)
ft_perror_exit("PID does not exist\n");
sigemptyset(&sa.sa_mask);
sa.sa_handler = ft_client_sighandler;
sa.sa_flags = SA_RESTART;
ft_set_sigaction(&sa);
ft_sep_color('0', '=', 28, GRN);
ft_printf("Sending to Server\n%sPID: %d%s\n", YEL, ft_atoi(argv[1]), NC);
ft_sep_color('0', '=', 28, GRN);
ft_send_msg(ft_atoi(argv[1]), argv[2]);
return (EXIT_SUCCESS);
}
static void ft_client_sighandler(int sig)
{
if (sig == SIGUSR1)
ft_printf("%s*%s", YEL, NC);
else if (sig == SIGUSR2)
{
ft_printf("\n");
ft_sep_color('0', '=', 28, GRN);
ft_printf("Message successfully sent!\n");
ft_sep_color('0', '=', 28, GRN);
exit(EXIT_SUCCESS);
}
}
static void ft_send_msg(pid_t pid, char *msg)
{
int i;
int msglen;
i = 0;
{
msglen = ft_strlen(msg);
ft_printf("%sOutbound msg's length = %d%s\n", CYN, msglen, NC);
ft_send_int(pid, msglen);
ft_printf("\n%sSending Message%s\n", GRN, NC);
while (msg[i] != '\0')
ft_send_char(pid, msg[i++]);
ft_printf("\n");
ft_sep_color('0', '=', 28, GRN);
ft_printf("%sSending NULL Terminator\n", MAG, NC);
ft_sep_color('0', '=', 28, GRN);
ft_send_char(pid, '\0');
}
}