Created
June 14, 2023 12:51
-
-
Save arthurwolf/66ca16feeecdb41449c5f0613791c9e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <signal.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
pid_t server_pid; | |
int index = 0, bit_index = 0, checksum = 0; | |
char* str; | |
void resend(int sig) { | |
index = 0; | |
bit_index = 0; | |
checksum = 0; | |
printf("Resending...\n"); | |
} | |
int main(int argc, char **argv) { | |
if (argc != 3) { | |
printf("Usage: %s <server_pid> <message>\n", argv[0]); | |
exit(1); | |
} | |
server_pid = atoi(argv[1]); | |
str = argv[2]; | |
signal(SIGUSR1, resend); | |
kill(server_pid, SIGUSR1); | |
usleep(100); | |
while (1) { | |
if (str[index] == '\0') { | |
kill(server_pid, SIGUSR1); | |
usleep(100); | |
kill(server_pid, SIGUSR1); | |
index++; | |
continue; | |
} | |
if ((str[index] >> (7 - bit_index)) & 1) { | |
kill(server_pid, SIGUSR2); | |
checksum++; | |
} | |
else { | |
kill(server_pid, SIGUSR1); | |
} | |
usleep(100); | |
bit_index++; | |
if (bit_index == 8) { | |
bit_index = 0; | |
index++; | |
} | |
} | |
// Send checksum | |
for (int i = 0; i < 8; i++) { | |
if ((checksum >> (7 - i)) & 1) { | |
kill(server_pid, SIGUSR2); | |
} else { | |
kill(server_pid, SIGUSR1); | |
} | |
usleep(100); | |
} | |
// Wait for server response | |
pause(); | |
free(str); | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <signal.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
volatile sig_atomic_t received = 0; | |
char* str; | |
int index = 0, bit_index = 0, checksum = 0, calculated_checksum = 0; | |
pid_t client_pid; | |
void handler(int sig, siginfo_t *info, void *context) { | |
if (received == 0) { | |
client_pid = info->si_pid; | |
received = 1; | |
return; | |
} | |
if (sig == SIGUSR1) { | |
str[index] = str[index] << 1; | |
} | |
else { | |
str[index] = (str[index] << 1) | 1; | |
calculated_checksum++; | |
} | |
bit_index++; | |
if (bit_index == 8) { | |
index++; | |
bit_index = 0; | |
} | |
if (str[index] == '\0') { | |
if (checksum == calculated_checksum) { | |
printf("Received: %s\n", str); | |
kill(client_pid, SIGUSR1); | |
} else { | |
printf("Error in transmission\n"); | |
kill(client_pid, SIGUSR2); | |
} | |
received = 0; | |
} | |
} | |
int main() { | |
struct sigaction sa; | |
sigemptyset(&sa.sa_mask); | |
sa.sa_sigaction = handler; | |
sa.sa_flags = SA_SIGINFO; | |
sigaction(SIGUSR1, &sa, NULL); | |
sigaction(SIGUSR2, &sa, NULL); | |
str = (char*) malloc(100); | |
pause(); | |
while (1) { | |
pause(); | |
} | |
free(str); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment