版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、课程设计:FTP的设计与实现/*cHe nt c*/#include <winsock2.h>#include <stdio.h>#include <stdlib.h>#pragma comment (lib ,"ws2_32.lib")#define DEFAULT_PORT2302#define DEFAULT_BUFFER 2048#define DEFAULT_MESSAGE "This is a test of the emergency broadcasting system" char szServeri
2、p128,/ Server to connect toszMessage1024; / Message to send to severint iPort = DEFAULT_PORT; / Port on server to connect to/DWORD dwCount = DEFAULT_COUNT; / Number of times to send messageBOOL bSendOnly = FALSE;/ Send data only; don't receiveint dirfun();int getfun();int putfun();int pwdfun();i
3、nt cdfun();int mdfun();int delfun();int usafun();void usage()printf("usage: client -p:x -s:IP -n:x -onn");printf("-p:xRemote port to send ton");printf("-s:IPServer's IP address or hostnamen");printf("-n:xNumber of times to send messagen");printf("-oSe
4、nd messages only; don't receiven");ExitProcess(1);void ValidateArgs( int argc, char *argv)inti;for (i = 1; i < argc; i+)if (argvi0 = '-') | (argvi0 = '/')switch (tolower(argvi1)case 'p':/ Remote portif (strlen(argvi) > 3)iPort = atoi(&argvi3);break ;case
5、39;s':/ Serverif (strlen(argvi) > 3)strcpy(szServerip, &argvi3);break ;case 'n':/ Number of times to send messageif (strlen(argvi) > 3)/dwCount = atol(&argvi3);break ;case 'o':/ Only send message; don't receivebSendOnly = TRUE;break ;default :usage();break ;int
6、main( int argc, char *argv)WSADATA wsd;SOCKET sClient;char szBufferDEFAULT_BUFFER;intret;/unsigned inti;/int j;struct sockaddr_in server;struct hostent *host = NULL;char choice5,choice240;/ Parse the command line and load Winsock/argv1="-s:"strcpy(szServerip, &argv13);/Validat
7、eArgs(argc, argv);if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)printf("Failed to load Winsock library!n");return 1;strcpy(szMessage, DEFAULT_MESSAGE);/ Create the socket, and attempt to connect to the server/sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if (sClient = INVALID_SOCKET)p
8、rintf("socket() failed: %dn", WSAGetLastError();return 1;server.sin_family = AF_INET;server.sin_port = htons(iPort);printf("server.sin_port=%un",server.sin_port); server.sin_addr.s_addr = inet_addr(szServerip);/ If the supplied server address wasn't in the form/ "aaa.bbb
9、.ccc.ddd" it's a hostname, so try to resolve it/if (server.sin_addr.s_addr = INADDR_NONE)host = gethostbyname(szServerip);if (host = NULL)printf("Unable to resolve server: %sn", szServerip); return 1;CopyMemory (&server.sin_addr, host->h_addr_listO, host->h_length);if (c
10、onnect(sClient, ( struct sockaddr *)&server,sizeof (server) = SOCKET_ERROR)printf("connect() failed: %dn", WSAGetLastError();return 1;/显示接通信息/for(j=0;j<2;j+)/ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0);if (ret = 0)/ Graceful closereturn 0;else if (ret = SOCKET_ERROR)printf(&quo
11、t;recv() failed: %dn", WSAGetLastError();return 0;szBufferret = '0'printf("%sn",szBuffer);if(ret<15)ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0);if (ret = 0)/ Graceful closereturn 0;else if (ret = SOCKET_ERROR)printf("recv() failed: %dn", WSAGetLastError();ret
12、urn 0;szBufferret = '0'printf("%sn",szBuffer);printf("DEFAULT_BUFFER=%dn",DEFAULT_BUFFER);while (1)puts("");printf("ftp> ");scanf("%s", choice);if (strncmp(choice,"dir",3)=0|strncmp(choice,"DIR",2)=0)dirfun(sClient);con
13、tinue ;else if (strncmp(choice,"pwd",3)=0|strncmp(choice,"PWD",3)=0)pwdfun(sClient);continue ;else if (strncmp(choice,"?",1)=0)usafun(sClient); continue ;else if (strncmp(choice,"quit",4)=0|strncmp(choice,"QUIT",2)=0)break ;scanf("%s", choi
14、ce2);if (strncmp(choice,"get",3)=0|strncmp(choice,"GET",3)=0)getfun(sClient,choice2);continue ;else if (strncmp(choice,"put",3)=0|strncmp(choice,"PUT",3)=0)putfun(sClient,choice2);continue ;else if (strncmp(choice,"cd",2)=0|strncmp(choice,"CD&qu
15、ot;,2)=0)cdfun(sClient,choice2);continue ;else if (strncmp(choice,"md",2)=0|strncmp(choice,"MD",2)=0)mdfun(sClient,choice2);continue ;else if (strncmp(choice,"del",3)=0|strncmp(choice,"DEL",3)=0)delfun(sClient,choice2);continue ;/elseputs("输入错误,请重新输入"
16、;);fflush(stdin);fflush(stdin);printf("n");printf("n");closesocket(sClient);WSACIeanup();return 0;int dirfun(SOCKET sClient)int ret;char *MSG="dir$" char szBuffer80;strcpy(szMessage, MSG);ret = send(sClient, szMessage, strlen(szMessage), 0);if (ret = 0)return 1;else if
17、(ret = SOCKET_ERROR)printf("send() failed: %dn", WSAGetLastError();return 1;printf("Send %d bytesn", ret);printf("bSendOnly=%dn",bSendOnly);while (!bSendOnly)/读取流并显示/ret = recv(sClient, szBuffer, 80, 0);printf("%s",szBuffer);ret = recv(sClient, szBuffer, 80, 0
18、);if (ret = 0)/ Graceful closereturn 1;else if (ret = SOCKET_ERROR)printf("recv() failed: %dn", WSAGetLastError();return 1;szBufferret = '0'if(strncmp(szBuffer,"226 Close",strlen("226 Close")=0)break ;printf("%s",szBuffer);if (strncmp(szBuffer,"50
19、0 Syntax error",strlen("500 Syntax error")=0)break ;return 0;int getfun(SOCKET sClient, char filename40)int ret;FILE *fpre;char szBuffer80;szMessageO='O:strcat(szMessage, "get$");/strcat(szMessage, "");strcat(szMessage,filename);printf("MSG4=%cn",szMe
20、ssage4);szMessage0='g'szMessage1='e'szMessage2='t'/szMessage4='m'szMessage5='e'szMessage 6='.'szMessage7='t:/szMessage8='x'szMessage9='t'szMessage10='0'ret = send(sClient, szMessage, strlen(szMessage)+1, 0);if (ret = 0)retur
21、n 1;else if (ret = SOCKET_ERROR)printf("send() failed: %dn", WSAGetLastError();return 1;printf("Send %d bytesn", ret);ret = recv(sClient, szBuffer, 80, 0);/*if (ret = 0)/ Graceful closebreak;else if (ret = SOCKET_ERROR)printf("recv() failed: %dn", WSAGetLastError();brea
22、k;*/szBufferret = '0'printf("%sn",szBuffer);if (strncmp( szBuffer,"125 Transfering.",strlen("125 Transfering.") )=0)if ( (fpre=fopen(filename,"w") = NULL )printf("open errer");return 1; printf("bSendOnly=%dn",bSendOnly);while (!bSen
23、dOnly)/读取流并显示ret = recv(sClient, szBuffer, 80, 0);if (ret = 0)/ Graceful closereturn 1;else if (ret = SOCKET_ERROR)printf("recv() failed: %dn", WSAGetLastError();return 1;szBufferret = '0'printf("%s",szBuffer);if (strncmp(szBuffer,"226 Transfer",strlen("226
24、 Transfer")=O)break;if (strncmp(szBuffer,"500 Syntax error",strlen("500 Syntax error")=0)break; fprintf(fpre,"%s",szBuffer);printf("%sn",szBuffer);fclose(fpre);return 0;int putfun(SOCKET sClient, char filename40)int ret; /int i;FILE *fpse; /char *filename
25、;/char *MSG="get0me.txt"char szBuffer80,temp_buffer80;/sprintf(szMessage, "get0","te.txt0");szMessage0='0'strcat(szMessage, "put$");strcat(szMessage,filename);ret = send(sClient, szMessage, strlen(szMessage)+1, 0);if (ret = 0)return 0;else if (ret = SO
26、CKET_ERROR)printf("send() failed: %dn", WSAGetLastError();return 1;printf("Send %d bytesn", ret);filename="me.txt"printf("filename=%sn",filename);if ( (fpse=fopen(filename,"r") = NULL )printf("open errer");return 1;elseprintf("The file
27、 %s found,ready to transfer.n",filename);i=0;while (fgets(temp_buffer,80,fpse)!=NULL)sprintf(szBuffer,"%s",temp_buffer); send(sClient, szBuffer, 80, 0);sprintf(szBuffer, "226 Transfer completed. rn");ret = send(sClient, szBuffer, strlen(szBuffer), 0);fclose(fpse);return 0;in
28、t pwdfun(SOCKET sClient)int ret;char *MSG="pwd$"char szBuffer160;strcpy(szMessage, MSG);ret = send(sClient, szMessage, strlen(szMessage), 0);if (ret = 0)return 1;else if (ret = SOCKET_ERROR)printf("send() failed: %dn", WSAGetLastError();return 1;printf("Send %d bytes'n&q
29、uot;, ret); printf("bSendOnly=%dn",bSendOnly); while (!bSendOnly)/读取流并显示ret = recv(sClient, szBuffer, 160, 0);if (ret = 0)/ Graceful closereturn 1;else if (ret = SOCKET_ERROR)printf("recv() failed: %dn", WSAGetLastError();return 1;szBufferret = '0'printf("%sn",s
30、zBuffer);if(strncmp(szBuffer,"226 Close",strlen("226 Close")=0)break;if (strncmp(szBuffer,"500 Syntax error",strlen("500 Syntax error")=0)break;return 0;int cdfun(SOCKET sClient, char pathname40)int ret;szMessage0='0:strcat(szMessage, "cd$");strc
31、at(szMessage,pathname);ret = send(sClient, szMessage, strlen(szMessage)+1, 0);if (ret = 0)return 1;else if (ret = SOCKET_ERROR)printf("send() failed: %dn", WSAGetLastError();return 1;printf("Send %d bytesn", ret);return 0;int mdfun(SOCKET sClient, char pathname20)int ret; char sz
32、Buffer160;/char *MSG="md$"szMessageO='O:strcat(szMessage, "md$");/strcat(szMessage, "");strcat(szMessage,pathname);strcpy(szMessage, MSG);ret = send(sClient, szMessage, strlen(szMessage)+1,0);if (ret = 0)return 1;else if (ret = SOCKET_ERROR)printf("send() faile
33、d: %dn", WSAGetLastError();return 1;printf("Send %d bytesn", ret);while (!bSendOnly)/读取流并显示ret = recv(sClient, szBuffer, 80, 0);if (ret = 0)/ Graceful closereturn 1;else if (ret = SOCKET_ERROR)printf("recv() failed: %dn", WSAGetLastError();return 1;szBufferret = '0'p
34、rintf("%s",szBuffer);if(strncmp(szBuffer,"226 Close",strlen("226 Close")=0)break ;if (strncmp(szBuffer,"500 Syntax error",strlen("500 Syntax error")=0)break ;return 0;int delfun(SOCKET sClient, char name20)int ret; char szBuffer80;szMessageO='O:s
35、trcat(szMessage, "del$");strcat(szMessage,name);ret = send(sClient, szMessage, strlen(szMessage)+1,0);if (ret = 0)return 1;else if (ret = SOCKET_ERROR)printf("send() failed: %dn", WSAGetLastError();return 1;printf("Send %d bytesn", ret);while (!bSendOnly)ret = recv(sCli
36、ent, szBuffer, 80, 0);if (ret = 0)/ Graceful closereturn 1;else if (ret = SOCKET_ERROR)printf("recv() failed: %dn", WSAGetLastError();return 1;szBufferret = '0'if (strncmp(szBuffer,"del ok",strlen("del ok")=0)printf("del %s okn",name);break ;printf(&qu
37、ot;%s",szBuffer);if (strncmp(szBuffer,"500 Syntax error",strlen("500 Syntax error")=0)break ;return 0;int usafun()puts(”");puts("get :取远方的一个文件”); puts("put :传给远方一个文件"); puts("pwd :显示远主当前目录”); puts("dir :列出远方当前目录”); puts("md:在远方新建文件夹");
38、puts("cd:改变远方当前目录”);puts(”?:显示你提供的命令”); puts("quit :退出返回"); return 0;/int quit()*server.c*#include <stdio.h>#include <winsock.h>#include <stdlib.h>#pragma comment lib ,"ws2_32.lib")WSADATA wsd;char sbuffer80,rbuffer80;/send and receive buffersint n,bytes; /
39、countersSOCKET newsocket,ns_data;struct sockaddr_in remoteaddr;remoteaddr_data;int port_connect=0; /port connect flagchar path80=""char order100=""/SOCKET s_data_port;int sy_error=1; / use for indicate Syntax error/server functionsint sdirfun(SOCKET newsocket);int sgetfun(SOCKET
40、newsocket);int sputfun(SOCKET newsocket);int spwdfun(SOCKET newsocket);int scdfun(SOCKET newsocket);int smdfun(SOCKET newsocket);int sdelfun(SOCKET newsocket);void HandleError( char *func);/server functions end/MAINint main( int argc, char *argv)portstruct sockaddr_in localaddr; /local address struc
41、tureSOCKETs; /s_data;/welcome socket and welcome socket for data connection,and connection for connect to clientint addr_inlen; /address lenght variableif (WSAStartup(MAKEWORD(2,2), &wsd) != 0)WSACleanup();printf("WSAStartup failedn");memset(&localaddr,0,sizeof (localaddr); /clear
42、localaddrs = socket(PF_INET, SOCK_STREAM, 0);if (s <0)printf("socket failedn");localaddr.sin_family = AF_INET;if (argc = 2)localaddr.sin_port = htons(u_short)atoi(argv1);elselocaladdr.sin_port = htons(2302);localaddr.sin_addr.s_addr = INADDR_ANY;if (bind(s,( struct sockaddr *)(&loca
43、laddr),sizeof (localaddr) < 0)printf("Bind failed!n");/INFINITE LOOPwhile (1) / while loop 1/LISTENlisten(s,3);addr_inlen = sizeof (remoteaddr);/ACCEPT main connection (control connection)newsocket = accept(s,( struct sockaddr *)(&remoteaddr),& addr_inlen);%dif (newsocket = INVA
44、LID_SOCKET) break ;printf("connectedto%s at portn",inet_ntoa(remoteaddr.sin_addr),ntohs(localaddr.sin_port);/Respond with welcome message, FTP client requires thosesprintf(sbuffer,"200 Welcome rn");bytes = send(newsocket, sbuffer, strlen(sbuffer), 0);sprintf(sbuffer,"530 Log
45、 in rn");bytes = send(newsocket, sbuffer, strlen(sbuffer), 0);/INFINITE LOOPwhile (1)/ while loop 2n = 0;sy_error=1;while (1)/ while loop 3/RECEIVEbytes = recv(newsocket, &rbuffern, 1,0); printf("rbuffer%d=%cn",n,rbuffern);if (bytes < 0) | (bytes = 0) break;if (rbuffern = '
46、$')rbuffern = '0' break;if (rbuffern != 'r')n+;/ end of while loop 3if (bytes < 0) | (bytes = 0)break;printf("#The Server receives:# '%s' from client n", rbuffer);/THE FTP COMMANDS HERE/LISTif (strncmp(rbuffer,"dir",3)=0)sdirfun(newsocket);/当前目录if(s
47、trncmp(rbuffer,"pwd",3)=0)spwdfun(newsocket);/改变目录if(strncmp(rbuffer,"cd",2)=0)scdfun(newsocket);/GETif(strncmp(rbuffer,"get",3)=0)sgetfun(newsocket);/PUTif(strncmp(rbuffer,"put",3)=0)sputfun(newsocket);MDif (strncmp(rbuffer,"md",2)=0)smdfun(newsocke
48、t);if (strncmp(rbuffer,"del",3)=0)sdelfun(newsocket);/QUITif (strncmp(rbuffer,"quit",4)=0)printf("quit n");sprintf(sbuffer, "221 Bye bye . rn");bytes = send(newsocket, sbuffer, strlen(sbuffer), 0);sy_error=0;break;/Syntax errorif (sy_error=1)printf("comma
49、nd unrecognized, non-implemented!n");sprintf(sbuffer, "500 Syntax error. n");bytes = send(newsocket, sbuffer, strlen(sbuffer), 0); / end of while loop 2/CLOSE CONTROL SOCKETclosesocket(newsocket);controlprintf("disconnectedfrom %s at port %d, closesocket.n",inet_ntoa(remotea
50、ddr.sin_addr),ntohs(localaddr.sin_port); / end of while loop 1/CLOSE WELCOME SOCKETclosesocket(s);printf("Welcome sockets close");return 0;int sdirfun(SOCKET newsocket)char temp_buffer80;printf("Equivalent to dir n");order0='0: strcat(order,"dir "); strcat(order,pat
51、h);strcat(order," >tmp.txt"); system(order); FILE *fin;fin=fopen("tmp.txt","r");sprintf(sbuffer, "125 Transfering. rn");bytes = send(newsocket, sbuffer, strlen(sbuffer), 0); while (fgets(temp_buffer,80,fin)!=NULL)sprintf(sbuffer,"%s",temp_buffer);
52、/if (port_connect=0) send(newsocket, sbuffer, strlen(sbuffer), 0);fclose(fin);sprintf(sbuffer, "226 Transfer completed. rn");bytes = send(newsocket, sbuffer, strlen(sbuffer), 0); system("del tmp.txt");/CLOSE the ns_data SOCKET or data port SOCKET if (port_connect=0)closesocket(ns
53、_data);sprintf(sbuffer,"226 Close the data socket. rn"); bytes = send(newsocket, sbuffer, strlen(sbuffer), 0); ns_data = socket(AF_INET, SOCK_STREAM, 0);sy_error=0;return 0;int sgetfun(SOCKET newsocket)printf("RETR mode.rn");int i=4,k=0;char filename20,temp_buffer80;/ identify th
54、e filename from rbuffer after the word "RETR ' while (1)/ while loop 4/RECEIVEbytes = recv(newsocket, &rbufferi, 1, 0); printf("rbufferi=%cn",rbufferi);if (bytes < 0) | (bytes = 0)break;filenamek=rbufferi;if (rbufferi = '0') /*end on LF*/filenamek = '0'break
55、;if (rbufferi != 'r')i+;k+; /*ignore CR's*/ / end of while loop 4order0='0:strcat(order,path);if (strlen(path)>0)strcat(order,"");strcat(order,filename);char *p_filename=order;FILE *fp;if ( (fp=fopen(p_filename,"r") = NULL )sprintf(sbuffer, "Sorry, cannot
56、open %s. Please try again.rn",filename);bytes = send(newsocket, sbuffer, strlen(sbuffer), 0);sprintf(sbuffer, "226 Transfer completed. rn");bytes = send(newsocket, sbuffer, strlen(sbuffer), 0);return 1;elseprintf("The file %s found,ready to transfer.n",filename);sprintf(sbuf
57、fer, "125 Transfering. rn");bytes = send(newsocket, sbuffer, strlen(sbuffer), 0);while (fgets(temp_buffer,80,fp)!=NULL)sprintf(sbuffer,"%s",temp_buffer);/if (port_connect=0)send(newsocket, sbuffer, strlen(sbuffer), 0); /end of whilefclose(fp);sprintf(sbuffer, "226 Transfer c
58、ompleted. rn"); bytes = send(newsocket, sbuffer, strlen(sbuffer), 0); sy_error=0;return 0;int sputfun(SOCKET newsocket)printf("Equivalent to put. n");int i=4,k=0;char filename20;/ identify the filename from rbuffer after the word "RETR 'while (1)bytes = recv(newsocket, &r
59、bufferi, 1, 0);if (bytes < 0) | (bytes = 0)break;filenamek=rbufferi;if (rbufferi = '0') /*end on LF*/filenamek = '0'break;if (rbufferi != 'r')i+;k+; /*ignore CR's*/ / end of whileorder0='0'strcat(order,path);if (strlen(path)>0)strcat(order,"");strcat(order,filename); printf("filena
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年规范格式员工解聘协议范本
- 2024年培训学校业务承接协议典范
- 2024年资格认证代理挂靠服务协议
- 2024年简化场地租赁协议范例
- 2024年水产养殖协议范本及条款详解
- DB11∕T 1694-2019 生活垃圾收集运输节能规范
- 2024年设备分期付款购销协议典范
- 2024年房产租赁业务协议参考
- 2024年停车场租赁模板协议
- 2024年度定制墙体租赁服务协议
- 朝花夕拾读书分享会
- 心肌病和心肌炎课件
- 突发事件应急处理知识培训
- 糖尿病专科护士考试试题
- 人工智能概论-人工智能概述
- 乡村旅游财务分析策划方案
- 高校学生事务管理1
- (中职)ZZ030植物病虫害防治赛项规程(7月19日更新)
- 2024年国能包神铁路集团有限责任公司招聘笔试参考题库附带答案详解
- 非甾体类抗炎药课件
- 出入库登记管理制度
评论
0/150
提交评论