FTP客户端和服务器源代码(C语言)实现要点.doc
《FTP客户端和服务器源代码(C语言)实现要点.doc》由会员分享,可在线阅读,更多相关《FTP客户端和服务器源代码(C语言)实现要点.doc(128页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateFTP客户端和服务器源代码(C语言)实现要点FTP客户端和服务器源代码(C语言)实现要点课程设计:FTP的设计与实现/*client.c*/#include #include #include #pragma comment(lib,ws2_32.lib)#define DEFAULT_PORT 2302#define DEFAULT_BUFFER 2048#defi
2、ne DEFAULT_MESSAGE This is a test of the emergency broadcasting systemcharszServerip128, / Server to connect to szMessage1024; / Message to send to severint iPort = DEFAULT_PORT;/ Port on server to connect to/DWORD dwCount = DEFAULT_COUNT; / Number of times to send messageBOOLbSendOnly = FALSE; / Se
3、nd data only; dont receiveint dirfun();int getfun();int putfun();int pwdfun();int cdfun();int mdfun();int delfun();int usafun();void usage() printf(usage: client -p:x -s:IP -n:x -onn); printf( -p:x Remote port to send ton); printf( -s:IP Servers IP address or hostnamen); printf( -n:x Number of times
4、 to send messagen); printf( -o Send messages only; dont receiven); ExitProcess(1);void ValidateArgs(int argc, char *argv) int i; for(i = 1; i 3) iPort = atoi(&argvi3); break; case s: / Server if (strlen(argvi) 3) strcpy(szServerip, &argvi3); break; case n: / Number of times to send message if (strle
5、n(argvi) 3) /dwCount = atol(&argvi3); break;case o: / Only send message; dont receive bSendOnly = TRUE; break; default: usage(); break; int main(int argc, char *argv) WSADATA wsd; SOCKET sClient; char szBufferDEFAULT_BUFFER; int ret; /unsigned int i; /int j; struct sockaddr_in server; struct hostent
6、 *host = NULL; char choice5,choice240; / Parse the command line and load Winsock / argv1=-s:127.0.0.1; strcpy(szServerip, &argv13); /ValidateArgs(argc, argv); if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) printf(Failed to load Winsock library!n); return 1; /strcpy(szMessage, DEFAULT_MESSAGE); / / Create
7、 the socket, and attempt to connect to the server / sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sClient = INVALID_SOCKET) printf(socket() failed: %dn, WSAGetLastError(); return 1; server.sin_family = AF_INET; server.sin_port = htons(iPort); printf(server.sin_port=%un,server.sin_port); s
8、erver.sin_addr.s_addr = inet_addr(szServerip);/ / If the supplied server address wasnt in the form / aaa.bbb.ccc.ddd its 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);
9、return 1; CopyMemory(&server.sin_addr, host-h_addr_list0, host-h_length); if (connect(sClient, (struct sockaddr *)&server, sizeof(server) = SOCKET_ERROR) printf(connect() failed: %dn, WSAGetLastError(); return 1; /显示接通信息 / / /for(j=0;j2;j+) / ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0); if (ret
10、 = 0) / Graceful close return 0; else if (ret = SOCKET_ERROR) printf(recv() failed: %dn, WSAGetLastError(); return 0; szBufferret = 0; printf(%sn,szBuffer); if(ret ); scanf(%s, choice); if(strncmp(choice,dir,3)=0|strncmp(choice,DIR,2)=0) dirfun(sClient); continue; else if(strncmp(choice,pwd,3)=0|str
11、ncmp(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, choice2); if(strncmp(choice,get,3)=0|strncmp(choice,GET,3)=0) getfun(sClient,choice2); continue; else if(strncmp(choi
12、ce,put,3)=0|strncmp(choice,PUT,3)=0) putfun(sClient,choice2); continue; else if(strncmp(choice,cd,2)=0|strncmp(choice,CD,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
13、,3)=0) delfun(sClient,choice2); continue; /else puts(输入错误,请重新输入); fflush(stdin); fflush(stdin); printf(n); printf(n); closesocket(sClient); WSACleanup(); return 0;int dirfun(SOCKET sClient) int ret; char *MSG=dir$;char szBuffer80; strcpy(szMessage, MSG); ret = send(sClient, szMessage, strlen(szMessa
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- FTP 客户端 服务器 源代码 语言 实现 要点
限制150内