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

📄 test2.c

📁 this gives details of the network programming
💻 C
字号:
// Netprog 2002 HW1 submission test program// This code tests layer 4//#include <stdio.h>#include <stdlib.h>#include <unistd.h>    // read() and write() prototypes are here//------------------------------------------------------------------// Sample layer 1 implementation based on a unix pipe.//int readfd,writefd;int generror=0;void init_l1(void) {  static int first=1;  int fds[2];  if (! first) {    close(readfd);    close(writefd);  } else {    first=0;  }  if (pipe(fds)==-1) {    printf("ERROR: Can't create pipe \n");    exit(1);  }  readfd = fds[0];  writefd = fds[1];}// sample l1_read just calls read on stdinint l1_read( char *b) {  if (generror==1) {    return(-1);  } else {    if (read(readfd,b,1)!=1)       return(-1);    else      return(1);  }}// sample l1_write just calls write to stdoutint l1_write(char b) {  if (generror==2) {    return(-1);  } else {    if (write(writefd,&b,1)!=1)       return(-1);    else      return(1);  }}//------------------------------------------------------------------// layer2 prototypesint l2_write(char *, int);int l2_read(char *, int);int main(int argc,char **argv) {  char buf[4000];  char inbuf[4000];  int len;  int err=0;  int i;  init_l1();  printf("l2 test 1 (sending ints): ");  for (i=0;i<1000;i++) {    if (l2_write((char *) &i, sizeof(int) )==-1) {      printf("l2_write error\n");      err=1;   break;    }    if ( (len=l2_read(buf,100))==-1) {      printf("l2_read error\n");      err=1;   break;    }    if (len!=sizeof(int)) {      printf("l2_read returns wrong length\n");      err=1;break;    }    if (*((int *)buf) != i) {      printf("l2_read value is wrong\n");      err=1;break;    }  }  if (! err) printf("OK\n");  init_l1(); err=0;  for (i=0;i<3000;i++)     buf[i]=i%256;  printf("l2 length test 999: ");  len=l2_write(buf,999);  if (len==-1) {      printf("l2_write error\n");      err=1;  } else {    if (len != 999) {      printf("l2_write returned wrong count\n");      err=1;    } else {      if ( (len=l2_read(inbuf,2000))==-1) {	printf("l2_read error\n");	err=1;      } else {	if (len!=999) {	  printf("l2_read returns wrong length\n");	  err=1;	} else {	  for (i=0;i<999;i++) {	    if (inbuf[i] != (char)(i%256)) {	      printf("l2_read value is wrong\n");	      err=1;break;	    }	  }	}      }    }  }  if (! err) printf("OK\n");  init_l1(); err=0;  printf("l2 forced read error: ");  init_l1();  generror=1;  if ( (len=l2_read(buf,100))==-1) {    printf("OK\n");  } else {    printf("doesn't return error (it should!)\n");  }  printf("l2 forced write error: ");  init_l1();  generror=2;  if ( (len=l2_write(buf,100))==-1) {    printf("OK\n");  } else {    printf("doesn't return error (it should!)\n");  }  return(0);}      

⌨️ 快捷键说明

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