基于g_soap制作的数据下载器,传输速度只有600kb 需改进
服务端:
//结构体 class ns_DownInfo { char m_pBuffer[1024]; int m_nReadSize; bool m_bEof; }; struct ns_getDownInfoResponse {ns_DownInfo return_;}; int ns__DownFile(char *pcFileName, int nPosition, struct ns_getDownInfoResponse &r); int ns__GetHelloWorld(char * &GetHelloWorldReturn); //注册 //server.h /******************************** author: DuanYueming Creat: 2014/7/19 Creat: 2014:17:52 purpose:发送文件模块 *******************************/ #include "FileInfo.h" #include "ns.nsmap" //#include <fstream> // // // //class Server //{ //public: // Server::Server(int _port=5678); // ~Server(); // int ns__SendFile(struct soap*, char *FileName, int Position, struct ns_GetFileInfo &s); // /*char *GetFileName(char *_FilePath);*/ // void InitSoap(); // // //private: // int m_TotalSize; // char *m_FileName; // bool m_state; // int m_port; // int m_UpdataSize; // struct ns__SendFile *m_SendFile; // struct soap m_soap; //}; //server.cpp // #include "Server.h" // using std::ifstream; // Server::Server(int _port) // { // m_TotalSize=0; // m_FileName=NULL; // m_state=false; // m_port=_port; // m_UpdataSize=0; // } // // //char* Server::GetFileName(char *_FilePath) // //{ // // m_FileName=_FilePath; // // /* return _FilePath;*/ // //} // // // void Server::InitSoap() // { // soap_init(&m_soap); // SOCKET _state = soap_bind(&m_soap,NULL,m_port,100); // if (_state == INVALID_SOCKET) // { // printf("The Socket Bind Error!"); // exit(1); // } // printf("Socket Successful"); // for (;;) // { // int _accept_state = (int)soap_accept(&m_soap); // if (_accept_state < 0) // { // soap_print_fault(&m_soap, stderr); // exit(-1); // } // printf("Connect Successful"); // soap_serve(&m_soap); // soap_end(&m_soap); // } // // } // int Server::ns__SendFile(struct soap*, char *FileName, int Position, struct ns_GetFileInfo &s) // { // // ifstream _file; // // _file.open(FileName,std::ios::in | std::ios::binary); // if (_file.is_open()) // { // _file.seekg(Position); // m_TotalSize = _file.tellg(); // while (!_file.eof()) // { // _file.read(s.m_FileInfo.m_FileBuff,1024); // m_UpdataSize += _file.gcount(); // printf("Sending %02d%%",(m_UpdataSize-m_TotalSize)*100); // fflush(stdout); // printf("\r"); // // } // _file.close(); // // } // return 0; // } // //inline ns_FileInfo * soap_new_req_ns_FileInfo(struct soap *soap, char m_FileBuff[1024], int m_TotalSize, bool m_state) //{ // ns_FileInfo *_p = soap_instantiate_ns_FileInfo(soap, -1, NULL, NULL, NULL); //if (_p) //{ // _p->soap_default(soap); // _p->ns_FileInfo::m_FileBuff = m_FileBuff; // _p->ns_FileInfo::m_TotalSize = m_TotalSize; // _p->ns_FileInfo::m_state = m_state; //} // return _p; //}
//客户端
#include "soapStub.h" #include "ns.nsmap" #include <fstream> using std::ofstream; using std::ifstream; int GetHelloWorld(const char* server) { struct soap str_soap; int nResult = 0; soap_init(&str_soap); char* buff;// = new char[1024]; soap_call_ns__GetHelloWorld(&str_soap, server, "", buff); if (str_soap.error) { soap_print_fault(&str_soap, stderr); } soap_end(&str_soap); soap_done(&str_soap); return 0; } int DownFile(const char* server) { //从服务器读取pSourceFileName文件另存为pOldFileName文件 char *pSourceFileName = "D:\\shrike-i386-disc1.iso", *pOldFileName = "E:\\444.txt"; struct soap str_soap; int nResult = 0; soap_init(&str_soap); struct ns_getDownInfoResponse rResponse; soap_call_ns__DownFile(&str_soap, server, "", pSourceFileName, 0, rResponse); bool bEof(false); if (str_soap.error) { soap_print_fault(&str_soap, stderr); bEof = true; } ofstream file; if(!bEof) { file.open(pOldFileName, std::ios::out | std::ios::binary | std::ios::trunc); if(file.is_open()) file.write(rResponse.return_.m_pBuffer, rResponse.return_.m_nReadSize); } bEof = rResponse.return_.m_bEof; int nPosition = rResponse.return_.m_nReadSize; soap_end(&str_soap); soap_done(&str_soap); while(!bEof) { soap_call_ns__DownFile(&str_soap, server, "", pSourceFileName, nPosition, rResponse); if (str_soap.error) { soap_print_fault(&str_soap, stderr); bEof = true; } else { if(file.is_open()) file.write(rResponse.return_.m_pBuffer, rResponse.return_.m_nReadSize); bEof = rResponse.return_.m_bEof; nPosition += rResponse.return_.m_nReadSize; } soap_end(&str_soap); soap_done(&str_soap); } if(file.is_open()) file.close(); return 0; } int main(int argc, char * argv[]) { char *server = "http://localhost:8080"; GetHelloWorld(server); DownFile(server); return 0; }
代码比较残缺,从坏了的硬盘弄出来的。。。当然,还是能做个例子的,具体的就不介绍了,相信有过一定编程经验的都能轻松看懂。