天天看點

【轉】gSoap傳輸二進制資料

原文出處

https://www.cnblogs.com/lpxblog/p/4791798.html

最近使用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);      
【轉】gSoap傳輸二進制資料

伺服器端代碼:

【轉】gSoap傳輸二進制資料
#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;
}      
【轉】gSoap傳輸二進制資料

用戶端代碼:

【轉】gSoap傳輸二進制資料
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();//關閉檔案
}      
【轉】gSoap傳輸二進制資料

繼續閱讀