gSoap传输二进制数据

最近使用gSoap传输二进制数据,遇到问题。gSoap不能一次传输二进制数据。所以使用分包传送。

1 struct xsd_DwgInfo
2 {
3     char m_pBuffer[1024];//分包大小
4     int m_nReadSize;//
5     bool m_bEof;//此次是否完全读取数据
6 };
7 
8 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, 1024);//读取数据长度
    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, "", "1", 0, 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, "", "1", 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();//关闭文件
}
原文地址:https://www.cnblogs.com/lpxblog/p/4791798.html