您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > FTP客户端源代码C语言
/*********************************************************************ÎļþÃû:client.c˵Ã÷:¼òµ¥µÄftp¿Í»§¶Ë³ÌÐòÎļþ£¬°üº¬mainº¯Êý¼°get¡¢putµÈÃüÁî·¢Ëͺ¯Êý¡£**********************************************************************/#includewinsock2.h#includestdio.h#includestdlib.h#pragmacomment(lib,ws2_32.lib)#defineDEFAULT_SERV_PORT2416//·þÎñÆ÷µÄ¼àÌý¶Ë¿Ú#defineDEFAULT_BUFFER_SIZE2048//»º³åÇø³¤¶Ècharsz_ServIp[128];//·þÎñÆ÷µÄIPµØÖ·charsz_Msg2Snd[1024];//·¢¸ø·þÎñÆ÷¶ËµÄ×Ö·û´®intiPort=DEFAULT_SERV_PORT;//·þÎñÆ÷µÄ¼àÌý¶Ë¿ÚBOOLb_IsSendOnly=FALSE;//Ö»·¢ÏûÏ¢£¬²»ÊÕÏûÏ¢intdirfun(SOCKET);//dirÃüÁî´¦Àíº¯Êýintgetfun(SOCKETh_Socket4Cmd,charFileName[40]);//getÃüÁî´¦Àíº¯Êýintputfun(SOCKETh_Socket4Cmd,charFileName[40]);//putÃüÁî´¦Àíº¯Êýintpwdfun(SOCKET);//pwdÃüÁî´¦Àíº¯Êýintcdfun(SOCKETh_Socket4Cmd,charpathname[40]);//cdÃüÁî´¦Àíº¯Êýintmdfun(SOCKETh_Socket4Cmd,charDocName[20]);//mdÃüÁî´¦Àíº¯Êýintdelfun(SOCKETh_Socket4Cmd,charname[20]);//delÃüÁî´¦Àíº¯Êýinthelpfun();//helpÃüÁî´¦Àíº¯ÊýintiSynError=1;/***********************************************************************º¯ÊýÃû:main˵Ã÷:Ö÷º¯ÊýÊäÈë²ÎÊý:intargcÊäÈë²ÎÊý³¤¶Èchar*argv[]ÊäÈë²ÎÊý×Ö·ûÐÍÊý×é***********************************************************************/intmain(intargc,char**argv){WSADATAwsd;SOCKETh_Socket4Cmd;charszBuffer[DEFAULT_BUFFER_SIZE];intret;structsockaddr_inserver;structhostent*host=NULL;charCmdWords[5],CmdWords2[40];argv[1]=-s:127.0.0.1;strcpy(sz_ServIp,&argv[1][3]);if(WSAStartup(MAKEWORD(2,2),&wsd)!=0)//winsock³õʼ»¯{printf(FailedtoloadWinsocklibrary!\n);return1;}h_Socket4Cmd=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);if(h_Socket4Cmd==INVALID_SOCKET){printf(creatingsocketfailed,error_code:%d\n,WSAGetLastError());return1;}server.sin_family=AF_INET;server.sin_port=htons(iPort);server.sin_addr.s_addr=inet_addr(sz_ServIp);if(server.sin_addr.s_addr==INADDR_NONE){host=gethostbyname(sz_ServIp);if(host==NULL){printf(Unabletoresolveserver:%s\n,sz_ServIp);return1;}CopyMemory(&server.sin_addr,host-h_addr_list[0],host-h_length);}if(connect(h_Socket4Cmd,(structsockaddr*)&server,sizeof(server))==SOCKET_ERROR)//Á´½Óµ½·þÎñÆ÷¶Ë{printf(connectingtoserverfailed,error_num:%d\n,WSAGetLastError());return1;}//½ÓÊÕ·þÎñÆ÷»¶ÓÏûÏ¢²¢´òÓ¡µ½ÆÁÄ»ret=recv(h_Socket4Cmd,szBuffer,DEFAULT_BUFFER_SIZE,0);if(ret==0)return0;elseif(ret==SOCKET_ERROR){printf(recvfunctionfailed,error_num:%d\n,WSAGetLastError());return0;}szBuffer[ret]='\0';printf(%s\n,szBuffer);if(ret15){ret=recv(h_Socket4Cmd,szBuffer,DEFAULT_BUFFER_SIZE,0);if(ret==0)return0;elseif(ret==SOCKET_ERROR)return0;szBuffer[ret]='\0';printf(%s\n,szBuffer);}helpfun();//´òÓ¡ÃüÁîÁбíwhile(1){puts(------------------------------------------);printf(ftp);scanf(%s,CmdWords);//ÊäÈëÃüÁîɨÃè//ÊäÈëÃüÁîʶ±ðif(strncmp(CmdWords,dir,3)==0||strncmp(CmdWords,DIR,3)==0){dirfun(h_Socket4Cmd);continue;}elseif(strncmp(CmdWords,pwd,3)==0||strncmp(CmdWords,PWD,3)==0){pwdfun(h_Socket4Cmd);continue;}elseif(strncmp(CmdWords,?,1)==0){helpfun();continue;}elseif(strncmp(CmdWords,quit,4)==0||strncmp(CmdWords,QUIT,2)==0)break;scanf(%s,CmdWords2);if(strncmp(CmdWords,get,3)==0||strncmp(CmdWords,GET,3)==0){getfun(h_Socket4Cmd,CmdWords2);continue;}elseif(strncmp(CmdWords,put,3)==0||strncmp(CmdWords,PUT,3)==0){putfun(h_Socket4Cmd,CmdWords2);continue;}elseif(strncmp(CmdWords,cd,2)==0||strncmp(CmdWords,CD,2)==0){cdfun(h_Socket4Cmd,CmdWords2);continue;}elseif(strncmp(CmdWords,md,2)==0||strncmp(CmdWords,MD,2)==0){mdfun(h_Socket4Cmd,CmdWords2);continue;}elseif(strncmp(CmdWords,del,3)==0||strncmp(CmdWords,DEL,3)==0){delfun(h_Socket4Cmd,CmdWords2);continue;}else{puts(ÊäÈë´íÎ󣬰´?ºÅ»ñÈ¡°ïÖú£¬»òÖØÐÂÊäÈë!);fflush(stdin);printf(\n);}}closesocket(h_Socket4Cmd);WSACleanup();return0;}/***********************************************************************º¯ÊýÃû:dirfun˵Ã÷:°´ÐÒé¹æÔò¹¹½¨Ä¿Â¼²éѯÃüÁî²¢·¢Ë͵½·þÎñÆ÷ÊäÈë²ÎÊý:SOCKETh_Socket4Cmd£¬Í¨¹ý´Ësocket·¢ËÍÃüÁîµ½·þÎñÆ÷¡£***********************************************************************/intdirfun(SOCKETh_Socket4Cmd){intret;char*MSG=dir$;charszBuffer[80];strcpy(sz_Msg2Snd,MSG);ret=send(h_Socket4Cmd,sz_Msg2Snd,strlen(sz_Msg2Snd),0);if(ret==0)return1;elseif(ret==SOCKET_ERROR){printf(sendfuntionfailed,error_num:%d\n,WSAGetLastError());return1;}while(!b_IsSendOnly){//¶ÁÈ¡Á÷²¢ÏÔʾret=recv(h_Socket4Cmd,szBuffer,80,0);if(ret==0)return1;elseif(ret==SOCKET_ERROR){printf(recvfunctionfailed,error_num:%d\n,WSAGetLastError());return1;}szBuffer[ret]='\0';if(strncmp(szBuffer,226Close,strlen(226Close))==0)break;printf(%s,szBuffer);if(strncmp(szBuffer,500Syntaxerror,strlen(500Syntaxerror))==0)break;}return0;}/***********************************************************************º¯ÊýÃû:getfun˵Ã÷:°´ÐÒé¹æÔò¹¹½¨ÎļþÏÂÔØÃüÁî²¢·¢Ë͵½·þÎñÆ÷ÊäÈë²ÎÊý:SOCKETh_Socket4Cmd£¬Í¨¹ý´Ësocket·¢ËÍÃüÁîµ½·þÎñÆ÷¡£charFileName[40],ÓûÏÂÔØÎļþµÄÎļþÃû***********************************************************************/intgetfun(SOCKETh_Socket4Cmd,charFileName[40]){intret;FILE*fpre;charszBuffer[80];sz_Msg2Snd[0]='\0';strcat(sz_Msg2Snd,get$);strcat(sz_Msg2Snd,FileName);/
本文标题:FTP客户端源代码C语言
链接地址:https://www.777doc.com/doc-3324399 .html