




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
-.z.《Linu*網(wǎng)絡(luò)編程》課程設(shè)計(jì)班級::指導(dǎo)老師:一、設(shè)計(jì)背景Linu*操作系統(tǒng)作為一個(gè)開源的操作系統(tǒng)被越來越多的人所應(yīng)用,它的好處在于操作系統(tǒng)源代碼的公開化!只要是基于GNU公約的軟件你都可以任意使用并修改它的源代碼。通過這次課程設(shè)計(jì)能更好的學(xué)習(xí)網(wǎng)絡(luò)編程知識和掌握LINU*平臺上應(yīng)用程序設(shè)計(jì)開發(fā)的過程,將大學(xué)四年所學(xué)知識綜合運(yùn)用,為未來的工作學(xué)習(xí)打下基礎(chǔ)。二、設(shè)計(jì)目的1、學(xué)習(xí)epoll跟FTP被動模式2、掌握linu*基本命令,例如ls、cd、login;3、學(xué)會如何編譯、運(yùn)行三、環(huán)境要求1、centos64位操作系統(tǒng)2、gcc編譯器四、設(shè)計(jì)原理4.1客戶端客戶端程序的主要任務(wù)有以下3個(gè):(1)、分析用戶輸入的命令。(2)、根據(jù)命令向服務(wù)器發(fā)出請求(3)、接受服務(wù)器返回請求的結(jié)果客戶端為用戶提供了3種命令:(1)、get:從服務(wù)器下載文件(2)、list:列出客戶端當(dāng)前目錄的容(3)、quit離開4.2服務(wù)器端(1)、分析請求代碼。(2)、根據(jù)請求代碼做相應(yīng)的處理(3)、等待返回結(jié)果或者應(yīng)答信息五、軟件測試結(jié)果六、部分主代碼*include"ftserve.h"intmain(intargc,char*argv[]){ intsock_listen,sock_control,port,pid; if(argc!=2){ printf("usage:./ftserveport\n"); e*it(0); } port=atoi(argv[1]); //createsocket if((sock_listen=socket_create(port))<0){ perror("Errorcreatingsocket"); e*it(1); } while(1){ //waitforclientrequest //createnewsocketforcontrolconnection if((sock_control=socket_accept(sock_listen)) <0) break; //createchildprocesstodoactualfiletransfer if((pid=fork())<0){ perror("Errorforkingchildprocess"); }elseif(pid==0){ close(sock_listen); ftserve_process(sock_control); close(sock_control); e*it(0); } close(sock_control); } close(sock_listen); return0;}/***Sendfilespecifiedinfilenameoverdataconnection,sending*controlmessageovercontrolconnection*Handlescaseofnullorinvalidfilename*/voidftserve_retr(intsock_control,intsock_data,char*filename){ FILE*fd=NULL; chardata[MA*SIZE]; 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,MA*SIZE,fd); if(num_read<0){ printf("errorinfread()\n"); } //sendblock if(send(sock_data,data,num_read,0)<0) perror("errorsendingfile\n"); }while(num_read>0); //sendmessage:226:closingconn,filetransfersuccessful send_response(sock_control,226); fclose(fd); }}/***Sendlistoffilesincurrentdirectory*overdataconnection*Return-1onerror,0onsuccess*/intftserve_list(intsock_data,intsock_control){ chardata[MA*SIZE]; size_tnum_read; FILE*fd; intrs=system("ls-l|tail-n+2>tmp.t*t"); if(rs<0){ e*it(1); } fd=fopen("tmp.t*t","r"); if(!fd){ e*it(1); } /*Seektothebeginningofthefile*/ fseek(fd,SEEK_SET,0); send_response(sock_control,1);//starting memset(data,0,MA*SIZE); while((num_read=fread(data,1,MA*SIZE,fd))>0){ if(send(sock_data,data,num_read,0)<0){ perror("err"); } memset(data,0,MA*SIZE); } fclose(fd); send_response(sock_control,226); //send226 return0; }/***Opendataconnectiontoclient*Returns:socketfordataconnection*or-1onerror*/intftserve_start_data_conn(intsock_control){ charbuf[1024]; intwait,sock_data; //Waitforgo-aheadoncontrolconn if(recv(sock_control,&wait,sizeofwait,0)<0){ perror("Errorwhilewaiting"); return-1; } //Getclientaddress structsockaddr_inclient_addr; socklen_tlen=sizeofclient_addr; getpeername(sock_control,(structsockaddr*)&client_addr,&len); inet_ntop(AF_INET,&client_addr.sin_addr,buf,sizeof(buf)); //Initiatedataconnectionwithclient if((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[MA*SIZE]; charpassword[MA*SIZE]; char*pch; charbuf[MA*SIZE]; char*line=NULL; size_tnum_read; size_tlen=0; FILE*fd; intauth=0; fd=fopen(".auth","r"); if(fd==NULL){ perror("filenotfound"); e*it(1); } while((num_read=getline(&line,&len,fd))!=-1){ memset(buf,0,MA*SIZE); strcpy(buf,line); pch=strtok(buf,""); strcpy(username,pch); if(pch!=NULL){ pch=strtok(NULL,""); strcpy(password,pch); } //removeendoflineandwhitespace trimstr(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[MA*SIZE]; charuser[MA*SIZE]; charpass[MA*SIZE]; memset(user,0,MA*SIZE); memset(pass,0,MA*SIZE); memset(buf,0,MA*SIZE); //Waittorecieveusername if((recv_data(sock_control,buf,sizeof(buf)))==-1){ perror("recverror\n"); e*it(1); } inti=5; intn=0; while(buf[i]!=0) user[n++]=buf[i++]; //tellclientwe'rereadyforpassword send_response(sock_control,331); //Waittorecievepassword memset(buf,0,MA*SIZE); if((recv_data(sock_control,buf,sizeof(buf)))==-1){ perror("recverror\n"); e*it(1); } i=5; n=0; while(buf[i]!=0){ pass[n++]=buf[i++]; } return(ftserve_check_user(user,pass));}/***Waitformandfromclientand*sendresponse*Returnsresponsecode*/intftserve_recv_cmd(intsock_control,char*cmd,char*arg){ intrc=200; charbuffer[MA*SIZE]; memset(buffer,0,MA*SIZE); memset(cmd,0,5); memset(arg,0,MA*SIZE); //Waittorecievemand if((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{//invalidmand rc=502; } send_response(sock_control,rc); returnrc;}/***Childprocesshandlesconnectiontoclient*/voidftserve_process(intsock_control){ intsock_data; charcmd[5]; chararg[MA*SIZE]; //Sendwelemessage send_response(sock_control,220); //Authenticateuser if(ftserve_login(sock_control)==1){ send_response(sock_control,230); }else{ send_response(sock_control,430)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 植絨布蝴蝶結(jié)行業(yè)深度研究分析報(bào)告(2024-2030版)
- 2025年中國小型柴油機(jī)(非道路)行業(yè)發(fā)展監(jiān)測及投資戰(zhàn)略研究報(bào)告
- 中國節(jié)育環(huán)取放器行業(yè)市場發(fā)展前景及發(fā)展趨勢與投資戰(zhàn)略研究報(bào)告(2024-2030)
- 山西省長治市屯留縣第一中學(xué)校2025屆高一化學(xué)第二學(xué)期期末綜合測試模擬試題含解析
- 中國橡膠襯里行業(yè)調(diào)查研究報(bào)告
- 數(shù)據(jù)報(bào)表分析財(cái)務(wù)報(bào)告
- 交通事故未成年人賠償
- 簡述電工安全操作規(guī)程
- 落實(shí)安全生產(chǎn)主體責(zé)任的實(shí)施方案
- 2020-2025年中國純豬肉粉行業(yè)發(fā)展趨勢預(yù)測及投資戰(zhàn)略咨詢報(bào)告
- 2025年吉林省中考物理試卷真題及答案詳解(精校打印版)
- 江蘇省南京市六校聯(lián)合體2024-2025學(xué)年高一下學(xué)期期末調(diào)研測試歷史試題(含答案)
- 標(biāo)準(zhǔn)的編寫講課件
- 學(xué)堂在線 護(hù)理研究方法 期末考試答案
- 2025年湖南省中考英語試卷真題(含答案解析)
- 2025年法律職業(yè)資格考試民法專項(xiàng)練習(xí)卷:合同法真題解析及試題
- 2025年天津市中考英語真題試卷及答案
- 玻尿酸介紹課件
- 2025至2030年中國電子束曝光系統(tǒng)行業(yè)市場研究分析及發(fā)展前景研判報(bào)告
- 2025中國心肌病綜合管理指南要點(diǎn)解讀課件
- 2025屆重慶市梁平區(qū)英語七年級第二學(xué)期期末調(diào)研模擬試題含答案
評論
0/150
提交評論