73 lines
1.6 KiB
C
73 lines
1.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: o_ :::::: ::: */
|
|
/* client.c :+: / :+::+: :+: */
|
|
/* +:+ > +:++:+ +:+ */
|
|
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
|
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
|
/* Created: 2024/07/18 16:28:03 by whaffman #+#+# #+#+# #+# #+# | */
|
|
/* Updated: 2024/07/27 18:40:57 by whaffman ### ### ### ### / \ */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
#include "libft/libft.h"
|
|
|
|
#define SLEEP 300
|
|
|
|
static volatile int g_ack = 0;
|
|
|
|
int send_byte(int pid, unsigned char c)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
while (i < 8)
|
|
{
|
|
if ((c & 128) == 128)
|
|
kill(pid, SIGUSR1);
|
|
else
|
|
kill(pid, SIGUSR2);
|
|
c = c << 1;
|
|
usleep(SLEEP);
|
|
while (!g_ack)
|
|
pause();
|
|
g_ack = 0;
|
|
i++;
|
|
}
|
|
return (1);
|
|
}
|
|
|
|
int send_message(int pid, char *msg)
|
|
{
|
|
while (*msg)
|
|
{
|
|
send_byte(pid, *msg);
|
|
msg++;
|
|
}
|
|
send_byte(pid, '\0');
|
|
return (1);
|
|
}
|
|
|
|
void sig(int signum)
|
|
{
|
|
(void)signum;
|
|
g_ack = 1;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int pid;
|
|
|
|
if (argc != 3)
|
|
ft_putstr("Usage: ./client <pid> <message>");
|
|
pid = ft_atoi(argv[1]);
|
|
if (pid <= 1)
|
|
return (1);
|
|
g_ack = 0;
|
|
signal(SIGUSR1, sig);
|
|
send_message(pid, argv[2]);
|
|
return (0);
|
|
}
|