版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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", choic
14、e2);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&quo
15、t;,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,"500
19、 Syntax error",strlen("500 Syntax error")=0)break ;return 0;int getfun(SOCKET sClient, char 40)int ret;FILE *fpre;char szBuffer80;szMessage0='0:strcat(szMessage, "get$");/strcat(szMessage, "");strcat(szMessage,);printf("MSG4=%cn",szMessage4);szMessage
20、0='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)return 1;else if (ret
21、= 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();break;*/szBufferret =
22、 '0'printf("%sn",szBuffer);if (strncmp( szBuffer,"125 Transfering.",strlen("125 Transfering.") )=0)if( (fpre=fopen(,"w") = NULL )printf("open errer");return 1; printf("bSendOnly=%dn",bSendOnly);while (!bSendOnly)/读取流并显示ret = recv(sC
23、lient, 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 Transfer")=O)break;i
24、f (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 40)int ret; /int i;FILE *fpse; /char *;/char *MSG="get0me.txt"char szB
25、uffer80,temp_buffer80;sprintf(szMessage, "getO","te.txtO");szMessage0='0'strcat(szMessage, "put$");strcat(szMessage,);ret = send(sClient, szMessage, strlen(szMessage)+1, 0);if (ret = 0)return 0;else if (ret = SOCKET_ERROR)printf("send() failed: %dn", W
26、SAGetLastError();return 1;printf("Send %d bytesn", ret);/"me.txt"printf("n",);if( (fpse=fopen(,"r") = NULL )printf("open errer");return 1;elseprintf("The found,ready to transfer.'”',);i=0;while (fgets(temp_buffer,80,fpse)!=NULL)sprintf(s
27、zBuffer,"%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;int pwdfun(SOCKET sClient)int ret;char *MSG="pwd$"char szBuffer160;strcpy(szMessage, MSG);r
28、et = 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", ret); printf("bSendOnly=%dn",bSendOnly); while (!bSendOnly)/读取流并显示ret = recv(sClie
29、nt, 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",szBuffer);if(strncmp(szBuffer,"226 Close",strlen("226 Close")=0)break;if (strnc
30、mp(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$");strcat(szMessage,pathname);ret = send(sClient, szMessage, strlen(szMessage)+1, 0);if (ret = 0)return 1
31、;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 szBuffer160;/char *MSG="md$"szMessageO='O:strcat(szMessage, "md$");/strcat(s
32、zMessage, "");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() failed: %dn", WSAGetLastError();return 1;printf("Send %d bytesn", ret);while (!bSendOnly
33、)/读取流并显示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 Close",strlen("226 Close&qu
34、ot;)=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:strcat(szMessage, "del$");strcat(szMessage,name);ret = send(sClient, szMessage, strlen(sz
35、Message)+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(sClient, szBuffer, 80, 0);if (ret = 0)/ Graceful closereturn 1;else if (ret = SOCKET_ERROR)printf(&quo
36、t;recv() failed: %dn", WSAGetLastError();return 1;szBufferret = '0'if (strncmp(szBuffer,"del ok",strlen("del ok")=0)printf("del %s okn",name);break ;printf("%s",szBuffer);if (strncmp(szBuffer,"500 Syntax error",strlen("500 Syntax er
37、ror")=0)break ;return 0;int usafun()puts(”");puts("get :取远方的一个文件”); puts("put :传给远方一个文件"); puts("pwd :显示远主当前目录”); puts("dir :列出远方当前目录”); puts("md:在远方新建文件夹");puts("cd:改变远方当前目录”);puts(”?:显示你提供的命令”); puts("quit :退出返回"); return 0;/int quit()*se
38、rver.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; /countersSOCKET newsocket,ns_data;struct sockaddr_in remoteaddr;remoteaddr_data;int port_connect=
39、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 newsocket);int sputfun(SOCKET newsocket);int spwdfun(SOCKET newsocket);int scdfun(SOCKET newsock
40、et);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 structureSOCKETs; /s_data;/welcome socket and welcome socket for data connection,and connection for c
41、onnect to clientint addr_inlen; /address lenght variableif (WSAStartup(MAKEWORD(2,2), &wsd) != 0)WSACleanup();printf("WSAStartup failedn");memset(&localaddr,0,sizeof (localaddr); /clear localaddrs = socket(PF_INET, SOCK_STREAM, 0);if (s <0)printf("socket failedn");loca
42、laddr.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 *)(&localaddr),sizeof (localaddr) < 0)printf("Bind failed!n");/INFINITE LOOPwhile (1) / wh
43、ile loop 1/LISTENlisten(s,3);addr_inlen = sizeof (remoteaddr);/ACCEPT main connection (control connection)newsocket = accept(s,( struct sockaddr *)(&remoteaddr),& addr_inlen);%dif (newsocket = INVALID_SOCKET)break ;printf("connectedto%s at portn",inet_ntoa(remoteaddr.sin_addr),ntoh
44、s(localaddr.sin_port);/Respond with welcome message, requires thosesprintf(sbuffer,"200 Welcome rn");bytes = send(newsocket, sbuffer, strlen(sbuffer), 0);sprintf(sbuffer,"530 Log in rn");bytes = send(newsocket, sbuffer, strlen(sbuffer), 0);/INFINITE LOOPwhile (1)/ while loop 2n =
45、 0;sy_error=1;while (1)/ while loop 3/RECEIVEbytes = recv(newsocket, &rbuffern, 1,0); printf("rbuffer%d=%cn",n,rbuffern);/THE HERE/LIST/当前目录/改变目录/GET/PUTif (bytes < 0) | (bytes = 0)break;if (rbuffern = '$')rbuffern = '0'break;if (rbuffern != 'r')n+;/ end of w
46、hile loop 3if (bytes < 0) | (bytes = 0)break;printf("#The Server receives:# '%s' from client n", rbuffer);if (strncmp(rbuffer,"dir",3)=0)sdirfun(newsocket);if (strncmp(rbuffer,"pwd",3)=0)spwdfun(newsocket);if (strncmp(rbuffer,"cd",2)=0)scdfun(newsoc
47、ket);if (strncmp(rbuffer,"get",3)=0)sgetfun(newsocket);if (strncmp(rbuffer,"put",3)=0)sputfun(newsocket);/MDif (strncmp(rbuffer,"md",2)=0)smdfun(newsocket);if (strncmp(rbuffer,"del",3)=0)sdelfun(newsocket);/QUITif (strncmp(rbuffer,"quit",4)=0)printf(
48、"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("command unrecognized, non-implemented!n");sprintf(sbuffer, "500 Syntax error. n");bytes = send(newsocket, s
49、buffer, strlen(sbuffer), 0); / end of while loop 2/CLOSE CONTROL SOCKETclosesocket(newsocket);controlprintf("disconnectedfrom %s at port %d, closesocket.n",inet_ntoa(remoteaddr.sin_addr),ntohs(localaddr.sin_port); / end of while loop 1/CLOSE WELCOME SOCKETclosesocket(s);printf("Welcom
50、e sockets close");return 0;int sdirfun(SOCKET newsocket)char temp_buffer80;printf("Equivalent to dir n");order0='0: strcat(order,"dir "); strcat(order,path);strcat(order," >tmp.txt"); system(order); FILE *fin;fin=fopen("tmp.txt","r");spri
51、ntf(sbuffer, "125 Transfering. rn"); bytes = send(newsocket, sbuffer, strlen(sbuffer), 0); while (fgets(temp_buffer,80,fin)!=NULL)sprintf(sbuffer,"%s",temp_buffer);/if (port_connect=0) send(newsocket, sbuffer, strlen(sbuffer), 0);fclose(fin);sprintf(sbuffer, "226 Transfer co
52、mpleted. 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_data);sprintf(sbuffer,"226 Close the data socket. rn"); bytes = send(newsocket, sbuffer, strlen(sbuffer),
53、 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 20,temp_buffer80;/ identify the from rbuffer after the word "RETR " while (1)/ while loop 4/RECEIVEif (bytes < 0) | (bytes = 0)break;k=rbuffe
54、ri;if (rbufferi = '0') /*end on LF*/k = '0'break;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,);char *p_;FILE *fp;if ( (fp=fopen(p_,"r") = NULL )spr
55、intf(sbuffer, "Sorry, cannot open %s. Please try again.rn",);bytes = send(newsocket, sbuffer, strlen(sbuffer), 0);sprintf(sbuffer, "226 Transfer completed. rn");bytes = send(newsocket, sbuffer, strlen(sbuffer), 0);return 1;elseprintf("The found,ready to transfer.、"'
56、,);sprintf(sbuffer, "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) /end of whilefclose(fp);sprintf(sbuffer, "226 Transfer completed. rn"); bytes = s
57、end(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 20;/ identify the from rbuffer after the word "RETR "while (1)bytes = recv(newsocket, &rbufferi, 1, 0);if (bytes < 0) | (bytes = 0)b
58、reak;k=rbufferi;if (rbufferi = '0') /*end on LF*/k = '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,);printf(",",order);char *p_;FILE *fpse;if (
59、 (fpse=fopen(order,"w") = NULL )printf("open errer");return 1;elseprintf("The found,ready to transfer.'"',);while (1)/读取流并显示int ret;ret = recv(newsocket, rbuffer, 80, 0);if (ret = 0)/ Graceful closereturn 0;else if (ret = SOCKET_ERROR)printf("recv() failed: %dn", WSAGetLastError(); return 0;if (strncmp(rbuffer,&quo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 吉林师范大学《工程静力学》2021-2022学年第一学期期末试卷
- 外出考察安全预案
- 学校建设施工总承包管理方案
- 吉林师范大学《儿童文学阅读与批评》2021-2022学年第一学期期末试卷
- 高效砂石矿开发利用方案
- 学校校园监控系统维保合同
- 2024劳务派遣合同范本派遣合同范本2
- 农业产品供应商不使用有害物质协议书
- 食品配送服务流程优化方案
- 2024个人对个人借款合同范本
- (完整版)医疗器械网络交易服务第三方平台质量管理文件
- 13G322-1~4《钢筋混凝土过梁(2013年合订本)》
- 关于幼儿园小班反邪教安全教案
- 某单位物业服务项目投标方案
- 35KV场内集电线路工程强条执行检查表
- 【多旋翼无人机的组装与调试5600字(论文)】
- 减速机知识及维修课件
- 内部项目跟投协议书(正)
- 一年级下册英语教学设计-Unit 4 food Lesson1- 人教新起点
- 5000头奶牛养殖场新建项目环境评估报告书
- IATF16949-过程审核检查表-(含审核记录)-
评论
0/150
提交评论