📄 default.htm
字号:
#include <stdio.h>//------------------------------------------------------------------// 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 stdinint l1_read( char *b) { return(read(0,b,1));}// sample l1_write just calls write to stdoutint l1_write(char b) { return(write(1,&b,1));}//------------------------------------------------------------------// layer 4 prototypesint l4_write(char *, int , char *, int );int l4_read(char *, int *, char *, int *);//------------------------------------------------------------------// 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) { char namebuf[101]; char valuebuf[101]; int namelen; int valuelen; // If there are 2 command line arguments then this program will send // the first (argv[1]) as the username and the second as the password // It uses l4_write to send each as a named value... // If there are not 2 command line arguments, the program assumes it should // be a reader, so it calls l4_read to get the named values. if (argc!=3) { // I'm a reader - read the first named value // IMPORTANT: l4_read requires that namelen and valuelen have values that // indicate how large the buffers namebuf and valuebuf are. They must // be initialized before calling l4_read. namelen = 100; valuelen = 100; if (l4_read(namebuf,&namelen,valuebuf,&valuelen)==-1) { fprintf(stderr,"Reading error\n"); exit(1); } // we expect C strings (with null termination), but make sure each is null terminated // (what if a bad guy sent the message?) before using as // a C string namebuf[namelen]='\0'; valuebuf[valuelen]='\0'; // Our application level protocol expects the first value to // be the username - make sure the name sent matches this. if (strcmp(namebuf,"username")!=0) { fprintf(stderr,"Error - first value received was not named \"username\"\n"); exit(1); } // We got something named "username", print it out... printf("Username: %s\n",valuebuf); // now get the password // namelen and valuelen need to be re-initialized, otherwise they // will have the values that reflect what happened the last time we called // l4_read namelen = 100; valuelen = 100; if (l4_read(namebuf,&namelen,valuebuf,&valuelen)==-1) { fprintf(stderr,"Reading error\n"); exit(1); } // we expect a C strings - make sure each is null terminated // (what if a bad guy sent the message?) before using as // a C string namebuf[namelen]='\0'; valuebuf[valuelen]='\0'; // Our application level protocol expects this value to // be the password - make sure the name sent matches this. if (strcmp(namebuf,"password")!=0) { fprintf(stderr,"Error - secondt value received was not named \"password\"\n"); exit(1); } // OK - We got something named "password", print it out... printf("Password: %s\n",valuebuf); } else { // I'm a writer (argv == 3) // the first command line argument is the username, so send it // using l4_write. Make sure the username is not more than 100 bytes // (this is part of the application level protocol - usernames can't // be more than 100 bytes). if (strlen(argv[1])>100) { fprintf(stderr,"Error - username is too long\n"); exit(1); } // length is OK - send it // NOTE: why add one to the lengths? This way the receiver is sent the // terminating null. if (l4_write("username",strlen("username")+1,argv[1],strlen(argv[1])+1)==-1) { // something went wrong when sending fprintf(stderr,"Error sending username\n"); exit(1); } // the second command line argument is the password, so send it // using l4_write. Make sure the password is not more than 100 bytes // (this is part of the application level protocol - passwords can't // be more than 100 bytes). if (strlen(argv[2])>100) { fprintf(stderr,"Error - password is too long\n"); exit(1); } // length is OK - send it if (l4_write("password",strlen("password")+1,argv[2],strlen(argv[2])+1)==-1) { // something went wrong when sending fprintf(stderr,"Error sending password\n"); exit(1); } } return(0);}</PRE></TD></TR></TBODY></TABLE></CENTER><A name=deliv><H3>Deliverables</H3></A><P>You must provide us with one file for each of the 3 layers you will write. The layer 2 code must be in a file named <CODE>l2.c</CODE>, the layer 3 code in <CODE>l3.c</CODE> and the layer 4 code in <CODE>l4.c</CODE>. Note that each of these files should have both a read and write function (<CODE>l2.c</CODE> should have <CODE>l2_read()</CODE> and <CODE>l2_write()</CODE>, ...).</P><DIV class=in><P><B>NOTE:</B>If you've never split a C program among multiple files before - here is how you would build the program. Assuming you save the sample main/layer 1 code in a file named <CODE>hw1.c</CODE>,you can use gcc to build the program like this: <PRE>> gcc -o hw1 hw1.c l2.c l3.c l4.c</PRE><P>The executeable program produced will be <CODE>hw1</CODE>, so you could then test the sender with something like this: <PRE>> ./hw1 uname passwd</PRE></DIV><P>You must also include in your submission a file named <CODE>README</CODE> that includes your name and a brief description of your submission, including the name of each file submitted along with a one line description of what is in the file. You should also mention what Operating System you used to develop the code, and any special instructions for building your code, problems you had or anything else you think might be helpful to us. You do not need to provide any code for layer1 or a main program, we will supply our own when testing your code.</P><A name=grading><H3>Grading</H3></A><P>Your project will be tested to make sure it works properly - a large part of this testing will make sure that your functions generate errors (return -1) when appropriate. Here are the specifics of the grading:</P><TABLE border=0> <TBODY> <TR> <TH>layer 2</TH> <TD>15%</TD> <TR> <TR> <TH>layer 3</TH> <TD>25%</TD> <TR> <TR> <TH>layer 4</TH> <TD>35%</TD> <TR> <TR> <TH>Style/Code structure, etc.</TH> <TD>25%</TD> <TR></TR></TBODY></TABLE><P>NOTE: 25% of your homework grade depends on the how well we can understand your code.</P><P>The tests for each of your layers will be run independently, so errors in your layer 2 code will not propogate to your layer 3 code... </P><H3>Submitting your files</H3><P>Submission of your homework is via email, the general idea is to send an email message with all your files as attachments. There is an automated email submission system that will respond to your submission right away, so you will have a record that we got your files. </P><P>All projects must be submitted via email to <A href="mailto:netprog-submit@cs.rpi.edu"><I>netprog-submit@cs.rpi.edu</I></A>. The subject line of the submission message should contain a single number indicating the project number (1 for HW1, 2 for HW2, etc.). You must include your files as attachments, feel free to send a zip-file or a tar file. <P><B>Don't send compiled code!</B></P><P>You can expect a return email indicating receipt of your project submission immediately. This receipt will include a list of all the files that were successfully extracted by the submission script - please look over the receipt carefully to make sure your submission worked.</P><P><B>Multiple Submissions: </B>You can resubmit up to 10 times for each project, we will always grade the last submission received unless you tell us otherwise. </P></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -