信息安全产品开发实践实验报告_第1页
信息安全产品开发实践实验报告_第2页
信息安全产品开发实践实验报告_第3页
信息安全产品开发实践实验报告_第4页
信息安全产品开发实践实验报告_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、四 川 大 学 计 算 机 学 院、软 件 学 院实 验 报 告 学号:_1043111034_ 姓名:陈枝 专业:软件工程 班级:3班 第 九 周 课程名称 信息安全产品开发实践实验课时2实验项目并发服务器(一)实验时间实验目的 服务器模型多进程服务器模型 实验环境 X86,WindowsXP,VMware5.0,RedHat Linux 9.0实验内容(算法、程序、步骤和方法) 试验题目1自己编写程序实现远程控制系统中使用到函数popen功能;思路: 使用管道pipe(int f_des2)函数(参数f_des0用于读取管道,f_des1用于向管道写入数据),通过管道实现父子进程间通讯;

2、步骤:创建管道;创建子进程;在父进程中:关闭f_des1,使用wait操作与等待子进程,然后将管道中的数据读出打印显示;在子进程:关闭f_des0,将管道f_des1与标准输出进行重定向(dup2(f_des1,STDOUT_FILENO)),然后调用execvp()函数执行程序中接收到的命令;试验题目2修改远程控制服务器代码,使得服务器同时能够向多个用户提供服务数据记录和计算 程序源码:题目1:FILE *mypopen(char *cmd,char type) int pipefd2; /管道描述符 int pid_t; /进程描述符 if(type !=r & type != w) pr

3、intf(myopen() flag error/n); return NULL; if(pipe(pipefd)0) /建立管道 printf(myopen() pipe create error/n); return NULL; pid_t=fork(); /建立子进程 if(pid_t 0) return NULL; if(0 = pid_t) /子进程中. if(type = r) close(pipefd0); /此函数返回文件指针是用于读,因此,在子进程中应该将结果写入管道, /这样父进程返回的文件指针才能读,所以这里将读端关闭 dup2(pipefd1,STDOUT_FILENO

4、); /exec函数的执行结果将会通过标准输出写到控制台上, /但这里我们不需要在控制台输出,而是需要将结果返回,因此将标准输出重定向到管道写端 close(pipefd1); else close(pipefd1); dup2(pipefd0,STDIN_FILENO); close(pipefd0); char *argv = cmd,NULL; if(execvp(cmd,argv)0) /用exec族函数执行命令 return NULL; wait(0); /等待子进程返回 if(type=r) close(pipefd1); return fdopen(pipefd0,r); /由于

5、程序需要返回的参数是文件指针,因此需要用fdopen函数将描述符打开,其返回值为相应的文件指针 else close(pipefd0); return fdopen(pipefd1,w); 题目2:#include #include #include #include #include #include #include #define PORT 8900#define BUFSIZE 2048void processchild(int clientsoket);FILE *mypopen(char *cmd,char type) int pipefd2; /管道描述符 int pid_t;

6、/进程描述符 if(type !=r & type != w) printf(myopen() flag error/n); return NULL; if(pipe(pipefd)0) /建立管道 printf(myopen() pipe create error/n); return NULL; pid_t=fork(); /建立子进程 if(pid_t 0) return NULL; if(0 = pid_t) /子进程中. if(type = r) close(pipefd0); /此函数返回文件指针是用于读,因此,在子进程中应该将结果写入管道, /这样父进程返回的文件指针才能读,所以

7、这里将读端关闭 dup2(pipefd1,STDOUT_FILENO); /exec函数的执行结果将会通过标准输出写到控制台上, /但这里我们不需要在控制台输出,而是需要将结果返回,因此将标准输出重定向到管道写端 close(pipefd1); else close(pipefd1); dup2(pipefd0,STDIN_FILENO); close(pipefd0); char *argv = cmd,NULL; if(execvp(cmd,argv)0) /用exec族函数执行命令 return NULL; wait(0); /等待子进程返回 if(type=r) close(pipef

8、d1); return fdopen(pipefd0,r); /由于程序需要返回的参数是文件指针,因此需要用fdopen函数将描述符打开,其返回值为相应的文件指针 else close(pipefd0); return fdopen(pipefd1,w); int execute(char*command,char*buf)FILE *fp;int count;char commandbuf2056;if (NULL=command)|(NULL=buf)perror(command or buf is emptyn);return -1;count =0;memset(commandbuf,

9、0,2056);strcat(commandbuf,sh -c );strcat(commandbuf,command);fprintf(stderr,the command is %sn,commandbuf);char ar=r;if (NULL=(fp=mypopen(commandbuf,ar)perror(create pipe errorn);return -1;while (count2047) & (EOF!=(bufcount+=fgetc(fp);bufcount-1=0;return count;int main()int sockfd;struct sockaddr_i

10、n client;struct sockaddr_in server;int opt;int cnt;int addr_len=sizeof(client); /* The first stage:INITILIZE */memset(&client,0,sizeof(client);memset(&server,0,sizeof(server);sockfd=-1;opt=SO_REUSEADDR;/*The second stage:create listen socket */if (-1=(sockfd=socket(AF_INET,SOCK_STREAM,0)perror(creat

11、e socket errorn);return -1;setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt);/* The third stage:bind socket */server.sin_family=AF_INET;server.sin_addr.s_addr=htonl(INADDR_ANY);server.sin_port=htons(PORT);if (-1=bind(sockfd,(struct sockaddr*)&server,sizeof(server)perror(bind socket errorn);

12、close(sockfd);return -1;if(listen(sockfd,5)0)perror(listen socket errorn);exit(1);/* The fifth stage:creat connect socket */while(1)int clientsocket;clientsocket = accept(sockfd,(struct sockaddr*)&client,(socklen_t*)&addr_len);if(clientsocket0)close(clientsocket);continue;else if(pid=0)close(sockfd)

13、;processchild(clientsocket);exit(0);elseprintf(error: fork child process!);close(sockfd);void processchild(int clientsocket) char bufferBUFSIZE;int iDatanum;int cnt;char sendbufBUFSIZE;int sendnum;while(1)iDatanum=recv(clientsocket,buffer,BUFSIZE,0);if(0iDatanum)perror(error: Recv data!);exit(0);buf

14、feriDatanum=0;fprintf(stderr,the command is:%sn,buffer);if (0=strcmp(buffer,quit)fprintf(stderr,the client is quitn);close(clientsocket);exit(0);if (1=(cnt=execute(buffer,sendbuf)sprintf(sendbuf,the invalid command,please try againn);fprintf(stderr,the result is n%s,sendbuf);if (0=(sendnum=write(clientsocket,sendbuf,strlen(sendbuf)perror(the commucation errorn);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论