1

client :

char    *content = NULL;

void    send_to_server_bonus(int pid, char *string)
{
    size_t  lenstring;
    int     j;
    size_t  i;

    i = 0;
    lenstring = ft_strlen_bonus(string);
    while (i <= lenstring)
    {
        j = 0;
        while (j < 8)
        {
            if (((string[i] >> j) & 1) == 1)
                kill(pid, SIGUSR1);
            else
                kill(pid, SIGUSR2);
            j++;
            usleep(1000);
        }
        i++;
    }
 //ft_putstr(content);
}
#include <stdio.h>

void    sig1handler(int sum)
{
    sum = 0;
    content = "adfa";
    printf("The message is received\n");
    fflush(NULL);
}

int main(int argc, char **argv)
{
    if (argc == 3)
    {
        send_to_server_bonus(ft_atoi_bonus(argv[1]), argv[2]);
        signal(SIGUSR1, sig1handler);
    }
    return (0);
}

server :

int signalPid = 0;

void    sighandler_bonus(int sum, siginfo_t *info, void *context)
{
    static char c;
    static int  count;

    context = NULL;
    signalPid = info->si_pid;
    if (sum == 31)
        sum = 0;
    if (sum == 30)
        sum = 1;
    c += (sum * ft_pow_bonus(2, count++));
    if (count == 8)
    {
        ft_putchar_bonus(c);
        if (!c)
        {
        ft_putnbr_bonus(signalPid);
        kill(signalPid,SIGUSR1);
        ft_putchar_bonus('\n');
        }
        c = 0;
        count = 0;
    }
}


int main(void)
{
    ft_putnbr_bonus(getpid());
    ft_putchar_bonus('\n');
    struct sigaction    sa;
    sa.sa_flags = SA_SIGINFO;
    sa.sa_sigaction = sighandler_bonus;
    while (1)
    {
        sigaction(SIGUSR2, &sa, NULL);
        sigaction(SIGUSR1, &sa, NULL);
        pause();
    }
}

The client sends a string to the server using signals SIGUSR1 AND SIGUSR2 (I used binaries to do that) : This part is working well

but the server has to send a signal back to the sender (client) when he received the string (kill (signalPId, SIGUSR1)) and the client has to catch it with sig1handler(int sum) : this is where i get this error "zsh: user-defined signal 1 " should i use sigaction() instead and how can I implement it .

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
xliben
  • 43
  • 4
  • Generally, using [`sigaction()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaction.html) rather than [`signal()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/signal.html) is a good idea. See [What is the difference between `sigaction()` and `signal()`?](https://stackoverflow.com/q/231912/15168) It isn't clear that this is the cause of your trouble, though. – Jonathan Leffler Dec 23 '21 at 15:55
  • You may be running into a timing problem. In the client, set the signal handler before you interact with the server, not after it: `{ signal(SIGUSR1, sig1handler); send_to_server_bonus(ft_atoi_bonus(argv[1]), argv[2]); }` instead of vice versa as now. – Jonathan Leffler Dec 23 '21 at 15:58

0 Answers0