最近使用gSoap传输二进制数据,遇到问题。gSoap不能一次传输二进制数据。所以使用分包传送。
struct xsd_DwgInfo
{
char m_pBuffer[];//分包大小
int m_nReadSize;//
bool m_bEof;//此次是否完全读取数据
}; int ns_GetDwgBinaryInfo(char* chMineID, int nPosition, struct xsd_DwgInfo& dwgInfo);
服务器端代码:
#include <fstream>
using std::ifstream; int ns_GetDwgBinaryInfo(struct soap*, char *chMineID, int nPosition, struct xsd_DwgInfo &dwgInfo)
{//分包传送二进制数据
ifstream file;
file.open("E:\\123.dwg", std::ios::in | std::ios::binary);
if(!file.is_open()) return SOAP_ERR; file.seekg(nPosition);//起始读取位置
file.read(dwgInfo.m_pBuffer, );//读取数据长度
dwgInfo.m_nReadSize = file.gcount();
dwgInfo.m_bEof = file.eof();//当前是否读取完毕
file.close(); return SOAP_OK;
}
客户端代码:
void CCClientWebServiceDemoDlg::OnBnClickedButton4()
{//分包传送
struct soap getInfoSoap;
soap_init(&getInfoSoap);
char* server_addr = "http://127.0.0.1:10000"; xsd_DwgInfo dwgInfo;
int nState = soap_call_ns_GetDwgBinaryInfo(&getInfoSoap, server_addr, "", "", , dwgInfo);
bool bEof(false);
if (getInfoSoap.error)
{
soap_print_fault(&getInfoSoap, stderr);
bEof = true;
}
ofstream file;
if(!bEof)
{
file.open(GetFilePath(_T("Circle.dwg")), std::ios::out | std::ios::binary | std::ios::trunc);
if(file.is_open())
file.write(dwgInfo.m_pBuffer, dwgInfo.m_nReadSize);
}
bEof = dwgInfo.m_bEof;
int nPosition = dwgInfo.m_nReadSize;
soap_end(&getInfoSoap);
soap_done(&getInfoSoap); while(!bEof)
{
soap_call_ns_GetDwgBinaryInfo(&getInfoSoap, server_addr, "", "", nPosition, dwgInfo);
if (getInfoSoap.error)
{
soap_print_fault(&getInfoSoap, stderr);
bEof = true;
}
else
{
if(file.is_open())
file.write(dwgInfo.m_pBuffer, dwgInfo.m_nReadSize);
bEof = dwgInfo.m_bEof;
nPosition += dwgInfo.m_nReadSize;
}
soap_end(&getInfoSoap);
soap_done(&getInfoSoap);
}
if(file.is_open()) file.close();//关闭文件
}