⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hw1.c

📁 this gives details of the network programming
💻 C
字号:
//// This code can be used to test your layer 4 implementation//   (which in turn should use your layer 3 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 <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);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -