/* This code can be used to test your layer 5 implementation (which in turn should use your layer 4 implementation, etc.) Included is a set of functions that can be used as a layer 1 implementation, and a main program. Question, suggestions, etc. should be sent to netprog@cs.rpi.edu */ #include /* printf, fprintf, etc */ #include /* need this for read(), write() */ #include /* needed for exit() */ #include /* strlen, etc */ /*------------------------------------------------------------------ Sample layer 1 implementation - this can be used to provide half-duplex communication by using the shell to create a pipe between a sender process and a receiver process. */ /* sample l1_read just calls read on stdin */ int l1_read( char *b) { return(read(0,b,1)); } /* sample l1_write just calls write to stdout */ int l1_write(char b) { return(write(1,&b,1)); } /* definition of student_rec */ typedef struct { char *firstname; char *lastname; int id; } student_rec; /* layer 5 prototypes */ int l5_write(student_rec *stu); int l5_read(student_rec *stu); /*------------------------------------------------------------------ Main program. This program can be a sender or a receiver, it depends on how many command line arguments are supplied when the program is run. */ int main(int argc,char **argv) { student_rec x; /* If there are 3 command line arguments then this program will send the first (argv[1]) as the "firstname" and the second as the "lastname" and the third as the id. It uses l5_write to send the structure. If there are not 3 command line arguments, the program assumes it should be a reader, so it calls l5_read to get the named value. */ if (argc!=4) { /* I'm a reader - read the structure */ if (l5_read(&x)==-1) { fprintf(stderr,"Reading error\n"); exit(1); } /* print out the record */ printf("Name: %s %s\n",x.firstname, x.lastname); printf("ID: %d\n",x.id); } else { /* I'm a writer (argv == 4) */ /* make sure strings are not too large */ if (strlen(argv[1])>80) { fprintf(stderr,"Error - first name is too long\n"); return(0); } if (strlen(argv[2])>80) { fprintf(stderr,"Error - last name is too long\n"); return(0); } /* the first command line argument is the firstname, the second is the lastname. */ x.firstname = argv[1]; x.lastname = argv[2]; /* argv[3] is the id */ x.id = atoi(argv[3]); /* send the struct */ if (l5_write(&x)==-1) { /* something went wrong when sending */ fprintf(stderr,"Error sending record\n"); exit(1); } } return(1); }