FTP_Client:#pragmacomment(lib,ws2_32.lib)#includewinsock.h#includeiostream.h#includestdio.h#includefstream.h#includestring.h#defineTRAN_SUCCESS1//传输成功#defineSOCK_WSA_ERR(-1)//启动winsock库失败#defineSOCK_CRSOCK_ERR(-2)//创建套接字失败#defineSOCK_BIND_ERR(-3)//绑定端口失败#defineSOCK_LISTEN_ERR(-4)//监听失败#defineSOCK_ACCEPT_ERR(-5)//等待连接失败#defineSOCK_SEND_ERR(-6)//发送数据失败#defineSOCK_CLOSE_ERR(-7)//关闭SOCKET失败#defineSOCK_RECVE_ERR(-8)//接受数据失败#defineSOCK_CONNECT_ERR(-10)#defineFILE_ERR(-9)//文件错误#defineOther_ERR(0)//其他不明原因#defineSVR_PORT5150//服务器端口#defineSEND_BUFFER_SIZE4096//每次发送大小#defineRECV_BUFFER_SIZE4096//每次发送大小structFileinfo{charfileName[50];//文件名intfileBlock;//文件分块大小intBlockNum;//文件块数量unsignedlongfileSize;//文件总大小};intRecevData(SOCKETs);intClient(char*strIP);voidPrintReturn(intnRet);intClient(char*strIP){intnResult=0;//1、启动Winsock:对WinsockDLL进行初始化,协商Winsock的版本支持并分配必要的资源。(服务器端和客户端)WORDwVersion=MAKEWORD(2,0);//wversion为winsock库的版本,高位字节为副版本,地位字节为主版本WSADATAwsData;nResult=WSAStartup(wVersion,&wsData);//加载winsock库,wsData参数是指向LPWSADATA结构的指针if(nResult!=0){returnSOCK_WSA_ERR;}//2、创建套接字:(服务器端和客户端)SOCKETs=socket(AF_INET,SOCK_STREAM,0);//创建套接字if(s==INVALID_SOCKET){returnSOCK_CRSOCK_ERR;//错误处理}unsignedlonguladdr;uladdr=inet_addr(127.0.0.1);//sock=socket(AF_INET,SOCK_STREAM,0);sockaddr_insa;memset(&sa,0,sizeof(sa));sa.sin_family=AF_INET;sa.sin_port=htons(5150);sa.sin_addr.S_un.S_addr=uladdr;nResult=connect(s,(constsockaddr*)&sa,sizeof(sa));//4、套接字的连结:将两个套接字连结起来准备通信。(客户端)/*intconnect(SOCKETs,conststructsockaddrFAR*name,intnamelen)s为欲连结的已创建的套接字。name为欲连结的socket地址。namelen为socket地址的结构的长度。*//*unsignedlonguladdr;uladdr=inet_addr(strIP);sockaddr_inaddr;memset(&addr,0x00,sizeof(addr));addr.sin_family=AF_INET;addr.sin_port=htons(SVR_PORT);//保证字节顺序addr.sin_addr.s_addr=htonl(uladdr);//保证字节顺序nResult=connect(s,(sockaddr*)&addr,sizeof(sockaddr));*/if(nResult0){returnSOCK_CONNECT_ERR;//错误处理}//6、套接字的数据接收:(客户端)nResult=RecevData(s);if(nResult0){returnnResult;//错误处理}//7、关闭套接字:释放所占有的资源。(服务器端和客户端)nResult=closesocket(s);if(nResult0){returnSOCK_CLOSE_ERR;//错误处理}returnTRAN_SUCCESS;}//---------------------------------------------------------------------------------intRecevData(SOCKETs){FILE*stream;structFileinfofileinfo;intnRet=0;unsignedlongdataLength,cbBytesRet,cbLeftToReceive;BYTE*recdData=NULL;memset(&fileinfo,0x00,sizeof(fileinfo));nRet=recv(s,(char*)&fileinfo,sizeof(fileinfo),0);if(nRet==SOCKET_ERROR){returnSOCK_RECVE_ERR;}if((stream=fopen(fileinfo.fileName,wb))==NULL){returnFILE_ERR;}//char*tt;//nRet=send(s,(char*)OK,sizeof(OK),0);dataLength=fileinfo.fileSize;recdData=newbyte[RECV_BUFFER_SIZE];cbLeftToReceive=dataLength;intii=0;do{intiiGet,iiRecd;iiGet=(cbLeftToReceiveRECV_BUFFER_SIZE)?cbLeftToReceive:RECV_BUFFER_SIZE;iiRecd=recv(s,(char*)recdData,iiGet,0);//testforerrorsandgetoutiftheyoccurredif(iiRecd==SOCKET_ERROR||iiRecd==0){returnSOCK_RECVE_ERR;}printf(%d\n,ii++);fwrite(recdData,iiRecd,1,stream);//WriteitcbLeftToReceive-=iiRecd;send(s,(char*)&ii,sizeof(ii),0);}while(cbLeftToReceive0);returnTRAN_SUCCESS;}voidPrintReturn(intnRet){switch(nRet){caseTRAN_SUCCESS:coutTRAN_SUCCESSendl;break;//传输成功caseSOCK_WSA_ERR:coutSOCK_WSA_ERRendl;break;//启动winsock失败caseSOCK_CRSOCK_ERR:coutSOCK_CRSOCK_ERRendl;break;//创建套接字失败caseSOCK_BIND_ERR:coutSOCK_BIND_ERRendl;break;//绑定端口失败caseSOCK_LISTEN_ERR:coutSOCK_LISTEN_ERRendl;break;//监听失败caseSOCK_ACCEPT_ERR:coutSOCK_ACCEPT_ERRendl;break;//等待连接失败caseSOCK_SEND_ERR:coutSOCK_SEND_ERRendl;break;//发送数据失败caseSOCK_CLOSE_ERR:coutSOCK_CLOSE_ERRendl;break;//关闭SOCKET失败caseSOCK_RECVE_ERR:coutSOCK_RECVE_ERRendl;break;//接受数据失败caseSOCK_CONNECT_ERR:coutSOCK_CONNECT_ERRendl;break;caseFILE_ERR:coutFILE_ERRendl;break;//文件错误caseOther_ERR:coutOther_ERRendl;break;//其他不明原因default:coutNOERRORendl;}}voidmain(){char*ip;cinip;intnRet=Client(ip);PrintReturn(nRet);coutRecvOK!!!endl;}FTP_Server:#pragmacomment(lib,ws2_32.lib)#includewinsock.h#includeiostream.h#includestdio.h#includefstream.h#includestring.h#defineTRAN_SUCCESS1//传输成功#defineSOCK_WSA_ERR(-1)//启动winsock失败#defineSOCK_CRSOCK_ERR(-2)//创建套接字失败#defineSOCK_BIND_ERR(-3)//绑定端口失败#defineSOCK_LISTEN_ERR(-4)//监听失败#defineSOCK_ACCEPT_ERR(-5)//等待连接失败#defineSOCK_SEND_ERR(-6)//发送数据失败#defineSOCK_CLOSE_ERR(-7)//关闭SOCKET失败#defineSOCK_RECVE_ERR(-8)//接受数据失败#defineFILE_ERR(-9)//文件错误#defineOther_ERR(0)//其他不明原因#defineSVR_PORT5150//服务器端口#defineSEND_BUFFER_SIZE4096//每次发送大小structFileinfo{charfileName[50];//文件名intfileBlock;//文件分块大小intBlockNum;//文件块数量unsignedlongfileSize;//文件总大小};boolGetLocalIP(charcaIpAddr[]);intFileSend(SOCKET*s,char*filename);voidPrintReturn(intnRet);longfilesize(FILE*stream);//----------------------------------------------------------------------------------intServerSend(char*FileName){intnResult=0;//1、启动Winsock:对WinsockDLL进行初始化,协商Winsock的版本支持并分配必要的资源。(服务器端和客户端)WORDwVersion=MAKEWORD(2,0);WSADATAwsData;nResult=WSAStartup(wVersion,&wsData);if(nResult!=0){returnSOCK_WSA_ERR;}//2、创建套接字:(服务器端和客户端)SOCKETs=socket(AF_INET,SOCK_STREAM,