Skip to main content
Code Review

Return to Question

Spelling and grammar
Source Link
Toby Speight
  • 87.7k
  • 14
  • 104
  • 325

Basic Client/Server model using UNIX SIGNALS in CUnix signals

Hello i'veI've created a basic client/server model using user defined signals (SIGUSR1SIGUSR1 representing bit1bit 1 and SIGUSR2SIGUSR2 for bit 0). At a high level the algoalgorithm looks something like:

  • The server reveals it'sits PID and starts listening.
  • The client taketakes a string from stdin encodestandard input, encodes it to binary and start streamingstarts streaming it to the pidserver byte by byte.
  • theThe server receivereceives the signals and decodes it from its side and then print itthem.
  • The server prints the received string.

The code:

server.c

server.c


client.c

client.c

encoder.c

encoder.c

decoder.c

decoder.c

The program is built using makeMake, and it functions as it supposed to. i'mI'm interested in judgments regarding my style (if there's one, probably not...) and error handling and performance or just anything you think should be or shouldn't be there, hope my comments helpful.

Basic Client/Server model using UNIX SIGNALS in C

Hello i've created a basic client/server model using user defined signals (SIGUSR1 representing bit1 and SIGUSR2 for bit 0). At a high level the algo looks something like:

  • The server reveals it's PID and starts listening.
  • The client take a string from stdin encode it to binary and start streaming it to the pid byte by byte.
  • the server receive the signals and decodes it from its side and then print it.

The code:

server.c


client.c

encoder.c

decoder.c

The program is built using make, it functions as it supposed to. i'm interested in judgments regarding my style (if there's one, probably not...) and error handling and performance or just anything you think should be or shouldn't be there, hope my comments helpful.

Basic Client/Server model using Unix signals

I've created a basic client/server model using user defined signals (SIGUSR1 representing bit 1 and SIGUSR2 for bit 0). At a high level the algorithm looks something like:

  • The server reveals its PID and starts listening.
  • The client takes a string from standard input, encodes it to binary and starts streaming it to the server byte by byte.
  • The server receives the signals and decodes them.
  • The server prints the received string.

server.c

client.c

encoder.c

decoder.c

The program is built using Make, and it functions as it supposed to. I'm interested in judgments regarding my style (if there's one, probably not...) and error handling and performance or just anything you think should be or shouldn't be there.

Source Link

Basic Client/Server model using UNIX SIGNALS in C

Hello i've created a basic client/server model using user defined signals (SIGUSR1 representing bit1 and SIGUSR2 for bit 0). At a high level the algo looks something like:

  • The server reveals it's PID and starts listening.
  • The client take a string from stdin encode it to binary and start streaming it to the pid byte by byte.
  • the server receive the signals and decodes it from its side and then print it.

The code:

server.c

#include <signal.h>
void handler(int signal)
{
 //handler is called at each signal so my storage variables need to persist their values.
 static int i;
 char c;
 static int sequence[8];
 if (signal == SIGUSR1)
 sequence[i++] = 1;
 else if (signal == SIGUSR2)
 sequence[i++] = 0;
 if (i == 8)
 {
 //we have a byte: decode, write and restart
 c = decoder(sequence);
 write(1, &c, 1);
 i = 0;
 }
}
int main(void)
{
 pid_t pid;
 pid = getpid();
 //put str to fd 1.
 putstr_fd("pid: ", 1);
 //put number as str to fd 1.
 putnbr_fd(pid, 1);
 putstr_fd("\n", 1);
 while (1)
 {
 signal(SIGUSR1, handler);
 signal(SIGUSR2, handler);
 pause();
 }
}

client.c

#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
//for the sake of not missing up my signals i used only and only write().
void sender(int *sequence, pid_t pid)
{
 int i = 0; 
 while (i < 8)
 {
 if (sequence[i] == 1)
 if (kill(pid, SIGUSR1) == -1)
 {
 write(1, "Oh the package didn't arrive. you're obligated for a refund.\n", 50);
 exit(EXIT_FAILURE);
 }
 else if (sequence[i] == 0)
 if (kill(pid, SIGUSR2) == -1)
 {
 write(1, "Oh the package didn't arrive. you're obligated for a refund.\n", 50);
 exit(EXIT_FAILURE);
 };
 i++;
 //Road traffic control for signals.
 usleep(500);
 }
 free(sequence);
}
int main(int argc, char **argv)
{
 pid_t pid;
 int *sequence;
 int i = 0;
 //invalid number of args.
 if (argc != 3)
 {
 write(1, "WTF... I'm not executing that.\n", 31);
 exit(EXIT_FAILURE);
 } 
 pid = atoi(argv[1]);
 while (argv[2][i])
 {
 sequence = encoder(argv[2][i]);
 sender(sequence, pid);
 i++;
 }
 return 0;
}

encoder.c

#include <stdlib.h>
int *encoder(char c)
{
 int *sequence = malloc(sizeof(*sequence) * 8);
 if (!sequence)
 {
 ft_putstr_fd("couldn't malloc... Give some space greedy\n", 1);
 exit(EXIT_FAILURE);
 }
 int i = 7;
 int decimal = (int)c;
 while (decimal)
 {
 sequence[i--] = decimal % 2;
 decimal /= 2;
 }
 //ok that's a glue code, i encoded and i filled what's left with zeros from MSB side​ some ascii's are 7 bits some are 6. i didn't want to guess. so i glued.
 ​while (i > -1)
 ​sequence[i--] = 0; 
 ​return sequence;
 ​}

decoder.c

#include <math.h>
char decoder(int *sequence)
{
 ​int i = 0;
 ​int n = 7;
 ​int result = 0; 
 ​while (i < 8)
 ​{
 ​result += (sequence[i] * pow(2, n));
 ​i++;
 ​n--;
 ​}
 ​return (char)result;
}

The program is built using make, it functions as it supposed to. i'm interested in judgments regarding my style (if there's one, probably not...) and error handling and performance or just anything you think should be or shouldn't be there, hope my comments helpful.

lang-c

AltStyle によって変換されたページ (->オリジナル) /