您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 家电行业 > 利用流套接字实现一个简单的远程控制系统
四川大学计算机学院、软件学院实验报告学号:1143111172姓名:柴承训专业:软件学院班级:09第3周课程名称信息安全产品开发实践实验课时1实验项目远程控制系统实验时间2013/9/27实验目的流套接字与数据报套接字回顾远程控制系统流程实验环境X86,Windows7,VMware9.0,RedHatEnterpriseLinux5.0实验内容(算法、程序、步骤和方法)利用流套接字实现一个简单的远程控制系统:客户端输入“quit”,客户端程序与服务器端程序打印退出信息,终止程序的执行;客户输入命令,客户端将命令通过流套接字发送给客户端,服务器执行收到的命令,并将结果发送到客户端显示;如果没有客户输入的命令,服务器发送命令非法信息,并在客户端显示该条信息。编写实验报告:列出程序运行状态、截图、配文字说明;独立完成,打印报告,报告语言为中文,不少于4页A4,封面上书写姓名、学号。(接上)实验内容(算法、程序、步骤和方法)修改代码:编译运行结果:源代码:udpclient.c/**=====================================================================================**Filename:udpclient.c**Description::Thisprogramisusedtodemostratehowtoimplementaudprpccontrolserver**Version:1.0*Created:2013骞?9鏈?5鏃?16鏃?6鍒?5绉?*Revision:none*Compiler:gcc**Author:Gangliang*Organization:ComputerScienceCollege,SichuanUniversity**=====================================================================================*/#includestdio.h#includesys/socket.h#includesys/types.h#includenetinet/in.h#includearpa/inet.h#definePORT8888#defineSIZE2048#includestdlib.h/**===FUNCTION======================================================================*Name:print_useage*Description:*=====================================================================================*/voidprint_usage(char*cmd){fprintf(stderr,Usage:%sipaddress\n,cmd);}/*-----endoffunctionprint_useage-----*//**===FUNCTION======================================================================*Name:main*Description:*=====================================================================================*/intmain(intargc,char*argv[]){intsockfd;structsockaddr_inclient;structsockaddr_inserver;intlen;intport;intrvalue;charbuffer[SIZE];charcmd[10];/*thesetwovarsusedtodealwithcdcommand*///charpath[2028];//initsockfd=-1;bzero(&client,sizeof(structsockaddr));bzero(&server,sizeof(structsockaddr));len=-1;rvalue=-1;port=PORT;memset(buffer,0,sizeof(buffer));bzero(cmd,10);//bzero(path,2048);if(2!=argc){print_usage(argv[0]);exit(-1);}//phrase1:Thecreationofthesocketsockfd=socket(AF_INET,SOCK_STREAM,0);if(-1==sockfd){fprintf(stderr,errorincreatingsocket\n);exit(-1);}//Communationwiththeserver.Attention:Clientshouldsenddatatotheserverfirst.//Setthedestination'saddressserver.sin_family=AF_INET;server.sin_addr.s_addr=inet_addr(argv[1]);server.sin_port=htons(port);if(-1==connect(sockfd,(structsockaddr*)&server,sizeof(server))){perror(connecterror\n);close(sockfd);return-1;}while(1){fprintf(stderr,TCP);fgets(buffer,SIZE,stdin);rvalue=write(sockfd,buffer,strlen(buffer),0);if(0==strcmp(buffer,quit\n)||(0==strcmp(buffer,QUIT\N))){fprintf(stderr,quit...\n);break;}if(0rvalue){fprintf(stderr,errorinreceivingdata\n);continue;}sscanf(buffer,%s,cmd);if((0==strcmp(cd,cmd))||(0==strcmp(CD,cmd)))continue;len=sizeof(structsockaddr);rvalue=read(sockfd,buffer,SIZE,0);if(0rvalue){buffer[rvalue]='\0';fprintf(stderr,%s\n,buffer);}else{continue;}}close(sockfd);returnEXIT_SUCCESS;}/*----------endoffunctionmain----------*/Udpserver.c/**=====================================================================================**Filename:udpserver.c**Description:Thisprogramisusedtodemostratehowtoimplementaudprpcserver**Version:1.0*Created:2013骞?9鏈?4鏃?20鏃?9鍒?2绉?*Revision:none*Compiler:gcc**Author:GangLiang*Organization:ComputerScienceCollege,SichuanUniverstiy**=====================================================================================*/#includestdio.h#includesys/socket.h#includesys/types.h#includenetinet/in.h#includeunistd.h#includestdlib.h#definePORT8888/*serverport*/#defineSIZE2048/*2KBUFFER*/intexec(char*command,char*result){FILE*in;intlen;charc;len=0;bzero(result,sizeof(result));in=popen(command,r);if(NULL==in){fprintf(stderr,errorincreateapipe\n);return-1;}while(((c=fgetc(in))!=EOF)&&(lenSIZE)){result[len]=c;len++;}result[len]='\0';pclose(in);if(0==len){sprintf(result,%scannotexecute\n,command);}returnlen;}/*-----endoffunctionexec-----*//**===FUNCTION======================================================================*Name:main*Description:*=====================================================================================*/intmain(intargc,char*argv[]){intsockfd;/*socket*/structsockaddr_inclient;/*ipaddressofclient*/structsockaddr_inserver;/*ipaddressofserver*/intlen;intport;intrvalue;charsend_buf[SIZE];/*theresultofbuffer*/charrecv_buf[SIZE];/*thecommandbuffer*/charcmd[10];/*thesetwovarusedtodealwithcdcommand*/charpath[2048];intopt;intconnected;//initsockfd=-1;bzero(&client,sizeof(structsockaddr));bzero(&server,sizeof(structsockaddr));len=-1;rvalue=-1;bzero(send_buf,SIZE);bzero(recv_buf,SIZE);port=PORT;bzero(cmd,10);bzero(path,2048);opt=SO_REUSEADDR;connected=-1;//phrase1:createsocket;sockfd=socket(AF_INET,SOCK_STREAM,0);/*generatethesocket*/if(-1==sockfd){fprintf(stderr,errorincreatingsocket\n);exit(-1);}setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));//phrase2:bindthesocketserver.sin_family=AF_INET;server.sin_port=htons(port);server.sin_addr.s_addr=htonl(INADDR_ANY);rvalue=bind(sockfd,(structsockaddr*)&server,sizeof(str
本文标题:利用流套接字实现一个简单的远程控制系统
链接地址:https://www.777doc.com/doc-6946132 .html