《FTP客户端和服务器源代码(C语言)实现要点(共26页).doc》由会员分享,可在线阅读,更多相关《FTP客户端和服务器源代码(C语言)实现要点(共26页).doc(26页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上课程设计:FTP的设计与实现/*client.c*/#include #include #include #pragma comment(lib,ws2_32.lib)#define DEFAULT_PORT 2302#define DEFAULT_BUFFER 2048#define DEFAULT_MESSAGE This is a test of the emergency broadcasting systemcharszServerip128, / Server to connect to szMessage1024; / Message to send t
2、o severint iPort = DEFAULT_PORT;/ Port on server to connect to/DWORD dwCount = DEFAULT_COUNT; / Number of times to send messageBOOLbSendOnly = FALSE; / Send data only; dont receiveint dirfun();int getfun();int putfun();int pwdfun();int cdfun();int mdfun();int delfun();int usafun();void usage() print
3、f(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 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) i
4、Port = atoi(&argvi3); break; case s: / Server if (strlen(argvi) 3) strcpy(szServerip, &argvi3); break; case n: / Number of times to send message if (strlen(argvi) 3) /dwCount = atol(&argvi3); break;case o: / Only send message; dont receive bSendOnly = TRUE; break; default: usage(); break; int main(i
5、nt argc, char *argv) WSADATA wsd; SOCKET sClient; char szBufferDEFAULT_BUFFER; int ret; /unsigned int i; /int j; struct sockaddr_in server; struct hostent *host = NULL; char choice5,choice240; / Parse the command line and load Winsock / argv1=-s:127.0.0.1; strcpy(szServerip, &argv13); /ValidateArgs(
6、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) printf(sock
7、et() 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 wasnt in the form / aaa.bbb.ccc.ddd its a hostname, so try to res
8、olve 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_list0, host-h_length); if (connect(sClient, (struct sockaddr *)&server, sizeof(server) = SOCKET
9、_ERROR) printf(connect() failed: %dn, WSAGetLastError(); return 1; /显示接通信息 / / /for(j=0;j2;j+) / ret = recv(sClient, szBuffer, DEFAULT_BUFFER, 0); if (ret = 0) / Graceful close return 0; else if (ret = SOCKET_ERROR) printf(recv() failed: %dn, WSAGetLastError(); return 0; szBufferret = 0; printf(%sn,
10、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|strncmp(choice,PWD,3)=0) pwdfun(sClient); continue; else if(strncmp(choice,?,1)=0) usafun(sClient); continue; else if(strncmp(choice,quit,4)=0|strncm
11、p(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(choice,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,choic
12、e2); 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; /else puts(输入错误,请重新输入); fflush(stdin); fflush(stdin); printf(n); printf(n); closesocket(sClient); WSACle
13、anup(); 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 (ret = SOCKET_ERROR) printf(send() failed: %dn, WSAGetLastError(); return 1; /printf(Send %d bytesn, ret);
14、 /printf(bSendOnly=%dn,bSendOnly); while(!bSendOnly) /读取流并显示 /ret = recv(sClient, szBuffer, 80, 0); /printf(%s,szBuffer); ret = recv(sClient, szBuffer, 80, 0); if (ret = 0) / Graceful close return 1; else if (ret = SOCKET_ERROR) printf(recv() failed: %dn, WSAGetLastError(); return 1; szBufferret = 0
15、; if(strncmp(szBuffer,226 Close,strlen(226 Close)=0) break; printf(%s,szBuffer); if(strncmp(szBuffer,500 Syntax error,strlen(500 Syntax error)=0) break; return 0;int getfun(SOCKET sClient,char filename40) int ret; FILE *fpre; char szBuffer80; szMessage0=0; strcat(szMessage, get$); /strcat(szMessage,
16、 ); strcat(szMessage,filename); /printf(MSG4=%cn,szMessage4); /szMessage0=g;szMessage1=e;szMessage2=t; / /szMessage4=m;szMessage5=e;szMessage6=.;szMessage7=t; / /szMessage8=x;szMessage9=t;szMessage10=0; ret = send(sClient, szMessage, strlen(szMessage)+1, 0); if (ret = 0) return 1; else if (ret = SOC
17、KET_ERROR) printf(send() failed: %dn, WSAGetLastError(); return 1; printf(Send %d bytesn, ret); ret = recv(sClient, szBuffer, 80, 0); /*if (ret = 0) / Graceful close break; else if (ret = SOCKET_ERROR) printf(recv() failed: %dn, WSAGetLastError(); break; */ szBufferret = 0; printf(%sn,szBuffer); if(
18、strncmp( szBuffer,125 Transfering.,strlen(125 Transfering.) )=0) if( (fpre=fopen(filename,w) = NULL ) printf(open errer); return 1; printf(bSendOnly=%dn,bSendOnly); while(!bSendOnly) /读取流并显示 ret = recv(sClient, szBuffer, 80, 0); if (ret = 0) / Graceful close return 1; else if (ret = SOCKET_ERROR) pr
19、intf(recv() failed: %dn, WSAGetLastError(); return 1; szBufferret = 0; /printf(%s,szBuffer); if(strncmp(szBuffer,226 Transfer,strlen(226 Transfer)=0) break; if(strncmp(szBuffer,500 Syntax error,strlen(500 Syntax error)=0) break; fprintf(fpre,%s,szBuffer); printf(%sn,szBuffer); fclose(fpre); return 0
20、;int putfun(SOCKET sClient,char filename40) int ret;/int i; FILE *fpse;/char *filename; /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);
21、if (ret = 0) return 0; else if (ret = SOCKET_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; else printf(The file %s found,ready to transfe
22、r.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;int pwdfun(SOCKET sClient) int ret; char *MSG=pwd$
23、; 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 bytesn, ret); printf(bSendOnly=%dn,bSendOnly); while(!bSendOnly) /读取流并显示 ret =
24、recv(sClient, szBuffer, 160, 0); if (ret = 0) / Graceful close return 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(strncmp(szBuffer,500 Syntax error,strlen(5
25、00 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; else if (ret = SOCKET_ERROR) printf(send() failed: %dn, WSAGetLastEr
26、ror(); return 1; printf(Send %d bytesn, ret); return 0;int mdfun(SOCKET sClient,char pathname20) int ret;char szBuffer160; /char *MSG=md$; szMessage0=0; strcat(szMessage, md$); /strcat(szMessage, ); strcat(szMessage,pathname); /strcpy(szMessage, MSG); ret = send(sClient, szMessage, strlen(szMessage)
27、+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 close return 1; else if (ret = SOCKET_ERROR) printf(recv() failed: %dn, WSAGetLastError(); return 1; szBufferret = 0; printf(%s,szBuffer); if(strncmp(szBuffer,226 Close