您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > linux网络编程课设报告
..《Linux网络编程》课程设计班级:姓名:指导老师:..一、设计背景Linux操作系统作为一个开源的操作系统被越来越多的人所应用,它的好处在于操作系统源代码的公开化!只要是基于GNU公约的软件你都可以任意使用并修改它的源代码。通过这次课程设计能更好的学习网络编程知识和掌握LINUX平台上应用程序设计开发的过程,将大学四年所学知识综合运用,为未来的工作学习打下基础。二、设计目的1、学习epoll跟FTP被动模式2、掌握linux基本命令,例如ls、cd、login;3、学会如何编译、运行三、环境要求1、centos64位操作系统2、gcc编译器四、设计原理4.1客户端客户端程序的主要任务有以下3个:(1)、分析用户输入的命令。(2)、根据命令向服务器发出请求(3)、接受服务器返回请求的结果客户端为用户提供了3种命令:(1)、get:从服务器下载文件(2)、list:列出客户端当前目录的内容(3)、quit离开4.2服务器端(1)、分析请求代码。(2)、根据请求代码做相应的处理(3)、等待返回结果或者应答信息..五、软件测试结果....六、部分主代码#includeftserve.hintmain(intargc,char*argv[]){intsock_listen,sock_control,port,pid;if(argc!=2){printf(usage:./ftserveport\n);exit(0);}port=atoi(argv[1]);//createsocketif((sock_listen=socket_create(port))0){perror(Errorcreatingsocket);exit(1);}while(1){//waitforclientrequest..//createnewsocketforcontrolconnectionif((sock_control=socket_accept(sock_listen))0)break;//createchildprocesstodoactualfiletransferif((pid=fork())0){perror(Errorforkingchildprocess);}elseif(pid==0){close(sock_listen);ftserve_process(sock_control);close(sock_control);exit(0);}close(sock_control);}close(sock_listen);return0;}/***Sendfilespecifiedinfilenameoverdataconnection,sending*controlmessageovercontrolconnection*Handlescaseofnullorinvalidfilename*/voidftserve_retr(intsock_control,intsock_data,char*filename){FILE*fd=NULL;chardata[MAXSIZE];size_tnum_read;fd=fopen(filename,r);if(!fd){//senderrorcode(550Requestedactionnottaken)send_response(sock_control,550);}else{//sendokay(150Filestatusokay)..send_response(sock_control,150);do{num_read=fread(data,1,MAXSIZE,fd);if(num_read0){printf(errorinfread()\n);}//sendblockif(send(sock_data,data,num_read,0)0)perror(errorsendingfile\n);}while(num_read0);//sendmessage:226:closingconn,filetransfersuccessfulsend_response(sock_control,226);fclose(fd);}}/***Sendlistoffilesincurrentdirectory*overdataconnection*Return-1onerror,0onsuccess*/intftserve_list(intsock_data,intsock_control){chardata[MAXSIZE];size_tnum_read;FILE*fd;intrs=system(ls-l|tail-n+2tmp.txt);if(rs0){exit(1);}fd=fopen(tmp.txt,r);..if(!fd){exit(1);}/*Seektothebeginningofthefile*/fseek(fd,SEEK_SET,0);send_response(sock_control,1);//startingmemset(data,0,MAXSIZE);while((num_read=fread(data,1,MAXSIZE,fd))0){if(send(sock_data,data,num_read,0)0){perror(err);}memset(data,0,MAXSIZE);}fclose(fd);send_response(sock_control,226);//send226return0;}/***Opendataconnectiontoclient*Returns:socketfordataconnection*or-1onerror*/intftserve_start_data_conn(intsock_control){charbuf[1024];intwait,sock_data;//Waitforgo-aheadoncontrolconnif(recv(sock_control,&wait,sizeofwait,0)0){perror(Errorwhilewaiting);return-1;}..//Getclientaddressstructsockaddr_inclient_addr;socklen_tlen=sizeofclient_addr;getpeername(sock_control,(structsockaddr*)&client_addr,&len);inet_ntop(AF_INET,&client_addr.sin_addr,buf,sizeof(buf));//Initiatedataconnectionwithclientif((sock_data=socket_connect(CLIENT_PORT_ID,buf))0)return-1;returnsock_data;}/***Authenticateauser'scredentials*Return1ifauthenticated,0ifnot*/intftserve_check_user(char*user,char*pass){charusername[MAXSIZE];charpassword[MAXSIZE];char*pch;charbuf[MAXSIZE];char*line=NULL;size_tnum_read;size_tlen=0;FILE*fd;intauth=0;fd=fopen(.auth,r);if(fd==NULL){perror(filenotfound);exit(1);}while((num_read=getline(&line,&len,fd))!=-1){memset(buf,0,MAXSIZE);strcpy(buf,line);..pch=strtok(buf,);strcpy(username,pch);if(pch!=NULL){pch=strtok(NULL,);strcpy(password,pch);}//removeendoflineandwhitespacetrimstr(password,(int)strlen(password));if((strcmp(user,username)==0)&&(strcmp(pass,password)==0)){auth=1;break;}}free(line);fclose(fd);returnauth;}/***Loginconnectedclient*/intftserve_login(intsock_control){charbuf[MAXSIZE];charuser[MAXSIZE];charpass[MAXSIZE];memset(user,0,MAXSIZE);memset(pass,0,MAXSIZE);memset(buf,0,MAXSIZE);//Waittorecieveusernameif((recv_data(sock_control,buf,sizeof(buf)))==-1){perror(recverror\n);exit(1);}inti=5;..intn=0;while(buf[i]!=0)user[n++]=buf[i++];//tellclientwe'rereadyforpasswordsend_response(sock_control,331);//Waittorecievepasswordmemset(buf,0,MAXSIZE);if((recv_data(sock_control,buf,sizeof(buf)))==-1){perror(recverror\n);exit(1);}i=5;n=0;while(buf[i]!=0){pass[n++]=buf[i++];}return(ftserve_check_user(user,pass));}/***Waitforcommandfromclientand*sendresponse*Returnsresponsecode*/intftserve_recv_cmd(intsock_control,char*cmd,char*arg){intrc=200;charbuffer[MAXSIZE];memset(buffer,0,MAXSIZE);memset(cmd,0,5);memset(arg,0,MAXSIZE);//Waittorecievecommandif((recv_data(sock_control,buffer,sizeof(buffer)))==-1){perror(recverror\n);..return-1;}strncpy(cmd,buffer,4);char*tmp=buffer+5;strcpy(arg,tmp);if(strcmp(cmd,QUIT)==0){rc=221;}elseif((strcmp(cmd,USER)==0)||(strcmp(cmd,PASS)==0)||(strcmp(cmd,LIST)==0)||(strcmp(cmd,RETR)==0)){rc=200;}else{//invalidcommandrc=502;}send_response(sock_control,rc);returnrc;}/***Childprocesshandlesconnectiontoclient*/voidftserve_process(intsock_control){intsock_data;charcmd[5];chararg[MAXSIZE];//Sendwelcomemessagesend_response(sock_control,220);//Authenticateuserif(ftserve_login(sock_control)==1){send_response(sock_contr
本文标题:linux网络编程课设报告
链接地址:https://www.777doc.com/doc-5809861 .html