您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > FTP服务器客户端源代码C语言
FTP服务器源代码:#includeWinsock.h#includewindows.h#includestdio.h#defineRECV_PORT200#defineSEND_PORT3000#pragmacomment(lib,wsock32.lib)SOCKETsock,sock1,sockdir,sockput,sockget;sockaddr_inServerAddr;sockaddr_inClientAddr;intAddrlen;//地址长度charfilename[20];//文件名charorder[10];//命令charrbuff[1024];//接收缓冲区charsbuff[1024];//发送缓冲区//初始化winsockDWORDStartSock(){WSADATAWSAData;if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0){printf(sockinitfail!\n);return(-1);}return(1);}DWORDCreateSocket(){sock=socket(AF_INET,SOCK_STREAM,0);/*建立套接字*/if(sock==SOCKET_ERROR){printf(sockcreatefail!\n);WSACleanup();return(-1);}ServerAddr.sin_family=AF_INET;ServerAddr.sin_addr.s_addr=htonl(INADDR_ANY);/*任意网络接口*/ServerAddr.sin_port=htons(RECV_PORT);if(bind(sock,(structsockaddrFAR*)&ServerAddr,sizeof(ServerAddr))==SOCKET_ERROR){printf(bindistheerror);return(-1);}return(1);}//发送文件intSendFileRecord(SOCKETdatatcps,WIN32_FIND_DATA*pfd)//usedtosendresponsetoclient{charfilerecord[MAX_PATH+32];FILETIMEft;FileTimeToLocalFileTime(&pfd-ftLastWriteTime,&ft);SYSTEMTIMElastwtime;FileTimeToSystemTime(&ft,&lastwtime);char*dir=pfd-dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY?DIR:;sprintf(filerecord,%04d-%02d-%02d%02d:%02d%5s%10d%-40s\n,lastwtime.wYear,lastwtime.wMonth,lastwtime.wDay,lastwtime.wHour,lastwtime.wMinute,dir,pfd-nFileSizeLow,pfd-cFileName);if(send(datatcps,filerecord,strlen(filerecord),0)==SOCKET_ERROR){printf(Erroroccurswhensendingfilelist!\n);return0;}return1;}intSendFileList(SOCKETdatatcps){HANDLEhff;WIN32_FIND_DATAfd;//搜索文件hff=FindFirstFile(*,&fd);if(hff==INVALID_HANDLE_VALUE)//发生错误{constchar*errstr=can'tlistfiles!\n;printf(listfileerror!\n);if(send(datatcps,errstr,strlen(errstr),0)==SOCKET_ERROR){printf(erroroccurswhensengingfilelist!\n);}closesocket(datatcps);return0;}BOOLfMoreFiles=TRUE;while(fMoreFiles){//发送此项文件信息if(!SendFileRecord(datatcps,&fd)){closesocket(datatcps);return0;}//搜索下一个文件fMoreFiles=FindNextFile(hff,&fd);}closesocket(datatcps);return1;}intSendFile(SOCKETdatatcps,FILE*file){printf(sendingfiledata..);for(;;)//从文件中循环读取数据并发送客户端{intr=fread(sbuff,1,1024,file);if(send(datatcps,sbuff,r,0)==SOCKET_ERROR){printf(losttheconnectiontoclient!\n);closesocket(datatcps);return0;}if(r1024)//文件传送结束break;}closesocket(datatcps);printf(done\n);return1;}//连接DWORDConnectProcess(){Addrlen=sizeof(sockaddr_in);if(listen(sock,5)0)//连接数?{printf(Listenerror);return(-1);}printf(Listening...\n);for(;;){sock1=accept(sock,(structsockaddrFAR*)&ClientAddr,&Addrlen);for(;;){memset(rbuff,0,1024);memset(sbuff,0,1024);if(recv(sock1,rbuff,1024,0)=0){break;}printf(\n);printf(获取并执行的命令为:);printf(rbuff);if(strncmp(rbuff,get,3)==0){strcpy(filename,rbuff+4);printf(filename);FILE*file;//处理下载文件请求file=fopen(filename,rb);//打开下载的文件if(file){sprintf(sbuff,getfile%s\n,filename);if(!send(sock1,sbuff,1024,0)){fclose(file);return0;}else{//创建额外数据连接传送数据if(!SendFile(sock1,file))return0;fclose(file);}}//fileelse//打开文件失败{strcpy(sbuff,can'topenfile!\n);if(send(sock1,sbuff,1024,0))return0;}//lost}//getif(strncmp(rbuff,put,3)==0){FILE*fd;intcount;strcpy(filename,rbuff+4);fd=fopen(filename,wb);if(fd==NULL){printf(openfile%sforwritefailed!\n,filename);return0;}sprintf(sbuff,putfile%s,filename);if(!send(sock1,sbuff,1024,0)){fclose(fd);return0;}while((count=recv(sock1,rbuff,1024,0))0)fwrite(rbuff,sizeof(char),count,fd);fclose(fd);}//putif(strncmp(rbuff,dir,3)==0){strcpy(sbuff,rbuff);send(sock1,sbuff,1024,0);SendFileList(sock1);}if(strncmp(rbuff,cd,2)==0){strcpy(filename,rbuff+3);strcpy(sbuff,rbuff);send(sock1,sbuff,1024,0);SetCurrentDirectory(filename);}closesocket(sock1);}//for2}//for1}intmain(){if(StartSock()==-1)return(-1);if(CreateSocket()==-1)return(-1);if(ConnectProcess()==-1)return(-1);return(1);}运行结果FTP客户端运行源代码:#includeWinsock.h#includewindows.h#includestdio.h#includetime.h#includeiostream.h#defineRECV_PORT200#defineSEND_PORT3000#pragmacomment(lib,wsock32.lib)SOCKETsock;charfilename[20];sockaddr_inServerAddr;charrbuff[1024];charsbuff[1024];charIIP[20];//处理help命令voidhelp(){cout*********************COMMANDShelp***********************endl****命令*******************作用*****************endl?或者'help'...........显示你提供的所有命令endldir...................显示远方当前目录的文件endlcd....................改变远方当前目录和路径endlcd的用法(进入下级目录):'cd路径名'endlcd的用法(进入上级目录):'cd..'endlget...................下载(接受)文件endlget的用法:'get文件名'endlput..................上传(发送)文件endlput的用法:'put文件名'endlquit...................退出endl*****************************************************endl;}//处理dir命令voidlist(SOCKETsockfd){intnRead;while(true){nRead=recv(sock,rbuff,1024,0);if(nRead==SOCKET_ERROR){printf(readresponseerror!\n);exit(1);}if(nRead==0)//数据读取结束break;//显示数据rbuff[nRead]='\0';printf(%s,rbuff);}}intSendFile(SOCKETdatatcps,FILE*file){printf(sendingfiledata..);for(;;)//从文件中循环读取数据并发送客户端{intr=fread(sbuff,1,1024,file);if(send(datatcps,sbuff,r,0)==SOCKET_ERROR){printf(lostthrconnectiontoclient!\n);closesocket(datatcps);return0;}if(r1024)//文件传送结束break;}closesocket(datatcps);printf(done\n);return1;}//初始化sco
本文标题:FTP服务器客户端源代码C语言
链接地址:https://www.777doc.com/doc-3324405 .html