2010-08-05 22 views
0

我正在研究一個應用程序,我需要通過網絡發送「文件名」,「文件大小」和場景。我創建了一個使用boost的服務器,現在讀入文件大小和名稱。C++,boost:如何通過網絡填充緩衝區並傳輸(圖像)文件數據?

我想知道如何用文件數據填充緩衝區(如有必要)以及如何將其傳輸到服務器。

這是我現在得到:

#ifndef OFXFILETRANSFERSENDH 
#define OFXFILETRANSFERSENDH 

#undef check // necessary to get Boost running on Mac 

#include <vector> 
#include <boost/asio.hpp> 
#include <boost/bind.hpp> 
#include <boost/shared_ptr.hpp> 
#include <boost/enable_shared_from_this.hpp> 

#include "ofxFileTransferConnection.h" 
using boost::asio::ip::tcp; 
class ofxFileTransferSend : public boost::enable_shared_from_this<connection> { 
public: 
    typedef boost::shared_ptr<ofxFileTransferSend> pointer; 
    static pointer create(
         boost::asio::io_service& rIOService 
         ,std::string sServer 
         ,const char* sPort) 
    { 
     return pointer(new ofxFileTransferSend(rIOService,sServer, sPort)); 
    } 


private: 
    //-------------------------------------------------------------- 
    ofxFileTransferSend(
        boost::asio::io_service &rIOService 
        ,const std::string sServer 
        ,const char* sPort 
    ) 
     :port(sPort) 
     ,socket_(rIOService) 
     ,resolver_(rIOService) 
    { 

     tcp::resolver::query query(sServer, sPort); 
     resolver_.async_resolve(
          query 
          ,boost::bind(
            &ofxFileTransferSend::handleResolve 
            ,this 
            ,boost::asio::placeholders::error 
            ,boost::asio::placeholders::iterator 
          ) 
     ); 
    } 


    //-------------------------------------------------------------- 
    void handleResolve(
     const boost::system::error_code &rError 
     ,tcp::resolver::iterator oEndPointIterator 
    ) 
    { 
     if (!rError) { 
      tcp::endpoint end_point = *oEndPointIterator; 
      socket_.async_connect(
           end_point 
           ,boost::bind(
            &ofxFileTransferSend::handleConnect 
            ,this 
            ,boost::asio::placeholders::error 
            ,++oEndPointIterator 
           ) 
      ); 
     } 
     else { 
      std::cout << "Error while resolving server: " << std::endl; 
     } 
    } 

    //-------------------------------------------------------------- 
    void handleConnect(
     const boost::system::error_code &rError 
     ,tcp::resolver::iterator rOEndPointIterator 
    ) 
    { 
     if(!rError) { 
      std::cout << "Connected to remote server!" << std::endl; 
      std::size_t size = 1235; 
      std::ostream send_data_stream(&send_data); 
      send_data_stream << "filename.jpg" << "\r\n"; 
      send_data_stream << size << "\r\n"; 

      boost::asio::async_write(
          socket_ 
          ,send_data 
          ,boost::bind(
           &ofxFileTransferSend::handleSendFileInfo 
           ,this 
           ,boost::asio::placeholders::error 
          ) 
      ); 
     } 
     else { 
      // @todo on failure retry! 
      std::cout << "Error connecting to ofxFileTransferServer:" << rError.message()<< std::endl; 
     } 
    } 


    //-------------------------------------------------------------- 
    void handleSendFileInfo(
     const boost::system::error_code &rError 
    ) 
    { 
     if(!rError) { 
      cout << "okay nice, send file data done!\n"; 
     } 
     else { 
      std::cout << "Error sending file info: " << rError.message() << std::endl; 
     } 
    } 

    tcp::resolver resolver_; 
    tcp::socket socket_; 
    boost::asio::streambuf send_data; 
    const char* port; 
}; 
#endif 

回答

1

如何只傾銷電線上的文件嗎? HTTP就是這麼做的。事實上,我發現你使用的協議幾乎與HTTP相同。將所有元數據以明文形式第一(名稱,大小等),放在一個空行作爲元數據(\ r \ n)的終止,現在你需要的是轉儲文件本身:

void handleSendFileInfo(
    const boost::system::error_code &rError 
) 
{ 
    if(!rError) { 
     std::ofstream fileData(fileName); 

     boost::asio::async_write(
         socket_ 
         ,fileData 
         ,boost::bind(
          &ofxFileTransferSend::handleSendFileData 
          ,this 
          ,boost::asio::placeholders::error 
         ) 
     ); 
    } 
    else { 
     std::cout << "Error sending file info: " << rError.message() << std::endl; 
    } 
} 
+0

嗨詹尼,非常感謝您的回覆!這會正確發送-all-數據嗎?或者是所有的數據都是以塊的形式發送的,我是否需要使用塊多次調用這個async_write()? – pollux 2010-08-05 18:26:26

+0

這應該是所有的數據。不需要其他任何東西。 – Gianni 2010-08-05 19:12:57

+0

嗨詹尼,我明天去試試..我的代碼在工作。但非常感謝!沒想到它那麼容易! – pollux 2010-08-05 19:58:13

相關問題