本文发表在 rolia.net 枫下论坛(1)main.c
#include "assign1.h"
int sTOr[2], rTOs[2];
pid_t receiverPID;
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("ass1 <filename>\n");
exit(1);
}
if (pipe(sTOr) || pipe(rTOs)) {
perror("main(): pipe: ");
exit(errno);
}
if ((receiverPID = fork()) < 0) {
perror("main(): fork: ");
exit(errno);
}
else if (receiverPID == 0) {
if (dup2(sTOr[0], fileno(stdin)) == -1 || dup2(rTOs[1], fileno(stdout)) == -1) {
perror("main(): dup2: ");
exit(errno);
}
close(sTOr[0]);
close(sTOr[1]);
close(rTOs[0]);
close(rTOs[1]);
receiver();
}
close(sTOr[0]);
close(rTOs[1]);
sender(argv[1]);
return 0;
}
short makeChecksum(char *str) {
int x = 0;
short sum = 0;
for (x = 0; x < 128; x++) {
sum += str[x];
}
return (sum%255);
}
(2)sender.c
#include "assign1.h"
#include "sender.h"
extern int sTOr[2];
extern int rTOs[2];
FILE *fp;
static FRAME sendFrame;
void catch_alarm (int sig)
{
printf("Test alarm!");
}
void sender(char filename[]) {
short blockNum = 0;
char reply[5];
sigignore(SIGINT);
sendFrame.SOH = 00000001;
sendFrame.blockNum=0;
if ((fp = fopen(filename, "r")) == NULL) {
perror("receiver(): fopen: ");
exit(errno);
}
while ((readFile(sendFrame.msg)) > 0) {
++sendFrame.blockNum;
sendFrame.blockNumComp = ~sendFrame.blockNum;
sendFrame.checksum = makeChecksum(sendFrame.msg);
printf("\nSENDER: blockNum = %d blockNumComp = %d checksum = %d\n", sendFrame.block
Num,
sendFrame.blockNumComp, sendFrame.checksum);
write(sTOr[1], &sendFrame, sizeof(FRAME));
signal(SIGALRM, catch_alarm);
alarm(1);
while(1) {
read(rTOs[0], reply, sizeof("ACK"));
printf("%s was received\n", reply);
if (!strcmp(reply, "NAK")) {
printf("SENDER2: blockNum = %d blockNumComp = %d checksum = %d\n",
sendFrame.blockNum,
sendFrame.blockNumComp, sendFrame.checksum);
write(sTOr[1], &sendFrame, sizeof(FRAME));
}
else if (!strcmp(reply, "ACK"))
break;
else
exit(12);
}
}
}
int readFile(char *str) {
int x;
for(x = 0; x < 128; x++)
str[x] = '\0';
return fread(str, sizeof(char), 128, fp);
}
(3)receiver.c
#include "assign1.h"
#include "receiver.h"
FRAME rFrame;
void receiver() {
sigset(SIGINT, makeError);
while (1) {
if (read(STDIN_FILENO, &rFrame, sizeof(FRAME)) > 0) {
sleep(1);
fprintf(stderr, "RECEIVER: blockNum = %d blockNumComp = %d checksum = %d\n
msg = %s\n", rFrame.blockNum,
rFrame.blockNumComp, rFrame.checksum, rFrame.msg);
if (!checkSOH(rFrame.SOH) ||
!checkBlockNum(rFrame.blockNum, rFrame.blockNumComp) ||
!checkChecksum(rFrame.msg, rFrame.checksum))
write(STDOUT_FILENO, "NAK", sizeof("NAK"));
/* printf("Test!!!"); */
else
write(STDOUT_FILENO, "ACK", sizeof("ACK"));
}
}
}
int checkSOH(char SOH){
return (SOH == 00000001);
}
int checkBlockNum(const short blockNum, const short blockNumComp) {
short newComp = ~blockNum;
if (newComp == blockNumComp)
return 1; // No error
return 0; // Error
}
int checkChecksum(char *str, const short checksum) {
if (makeChecksum(str) == checksum)
return 1; // No error
return 0; // Error
}
void makeError() {
int n = rand()%3;
switch (n) {
case 0:
rFrame.blockNum = 0;
fputs("\nCHANGED BLOCK NUMBER\n", stderr);
break;
case 1:
rFrame.checksum = 0;
fputs("\nCHANGED CHECKSUM\n", stderr);
break;
case 2:
rFrame.blockNumComp = 10;
fputs("\nCHANGED BLOCK NUMBER COMPLIEMENT\n", stderr);
break;
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
#include "assign1.h"
int sTOr[2], rTOs[2];
pid_t receiverPID;
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("ass1 <filename>\n");
exit(1);
}
if (pipe(sTOr) || pipe(rTOs)) {
perror("main(): pipe: ");
exit(errno);
}
if ((receiverPID = fork()) < 0) {
perror("main(): fork: ");
exit(errno);
}
else if (receiverPID == 0) {
if (dup2(sTOr[0], fileno(stdin)) == -1 || dup2(rTOs[1], fileno(stdout)) == -1) {
perror("main(): dup2: ");
exit(errno);
}
close(sTOr[0]);
close(sTOr[1]);
close(rTOs[0]);
close(rTOs[1]);
receiver();
}
close(sTOr[0]);
close(rTOs[1]);
sender(argv[1]);
return 0;
}
short makeChecksum(char *str) {
int x = 0;
short sum = 0;
for (x = 0; x < 128; x++) {
sum += str[x];
}
return (sum%255);
}
(2)sender.c
#include "assign1.h"
#include "sender.h"
extern int sTOr[2];
extern int rTOs[2];
FILE *fp;
static FRAME sendFrame;
void catch_alarm (int sig)
{
printf("Test alarm!");
}
void sender(char filename[]) {
short blockNum = 0;
char reply[5];
sigignore(SIGINT);
sendFrame.SOH = 00000001;
sendFrame.blockNum=0;
if ((fp = fopen(filename, "r")) == NULL) {
perror("receiver(): fopen: ");
exit(errno);
}
while ((readFile(sendFrame.msg)) > 0) {
++sendFrame.blockNum;
sendFrame.blockNumComp = ~sendFrame.blockNum;
sendFrame.checksum = makeChecksum(sendFrame.msg);
printf("\nSENDER: blockNum = %d blockNumComp = %d checksum = %d\n", sendFrame.block
Num,
sendFrame.blockNumComp, sendFrame.checksum);
write(sTOr[1], &sendFrame, sizeof(FRAME));
signal(SIGALRM, catch_alarm);
alarm(1);
while(1) {
read(rTOs[0], reply, sizeof("ACK"));
printf("%s was received\n", reply);
if (!strcmp(reply, "NAK")) {
printf("SENDER2: blockNum = %d blockNumComp = %d checksum = %d\n",
sendFrame.blockNum,
sendFrame.blockNumComp, sendFrame.checksum);
write(sTOr[1], &sendFrame, sizeof(FRAME));
}
else if (!strcmp(reply, "ACK"))
break;
else
exit(12);
}
}
}
int readFile(char *str) {
int x;
for(x = 0; x < 128; x++)
str[x] = '\0';
return fread(str, sizeof(char), 128, fp);
}
(3)receiver.c
#include "assign1.h"
#include "receiver.h"
FRAME rFrame;
void receiver() {
sigset(SIGINT, makeError);
while (1) {
if (read(STDIN_FILENO, &rFrame, sizeof(FRAME)) > 0) {
sleep(1);
fprintf(stderr, "RECEIVER: blockNum = %d blockNumComp = %d checksum = %d\n
msg = %s\n", rFrame.blockNum,
rFrame.blockNumComp, rFrame.checksum, rFrame.msg);
if (!checkSOH(rFrame.SOH) ||
!checkBlockNum(rFrame.blockNum, rFrame.blockNumComp) ||
!checkChecksum(rFrame.msg, rFrame.checksum))
write(STDOUT_FILENO, "NAK", sizeof("NAK"));
/* printf("Test!!!"); */
else
write(STDOUT_FILENO, "ACK", sizeof("ACK"));
}
}
}
int checkSOH(char SOH){
return (SOH == 00000001);
}
int checkBlockNum(const short blockNum, const short blockNumComp) {
short newComp = ~blockNum;
if (newComp == blockNumComp)
return 1; // No error
return 0; // Error
}
int checkChecksum(char *str, const short checksum) {
if (makeChecksum(str) == checksum)
return 1; // No error
return 0; // Error
}
void makeError() {
int n = rand()%3;
switch (n) {
case 0:
rFrame.blockNum = 0;
fputs("\nCHANGED BLOCK NUMBER\n", stderr);
break;
case 1:
rFrame.checksum = 0;
fputs("\nCHANGED CHECKSUM\n", stderr);
break;
case 2:
rFrame.blockNumComp = 10;
fputs("\nCHANGED BLOCK NUMBER COMPLIEMENT\n", stderr);
break;
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net