gpt4 book ai didi

C++, boost : how to fill buffer and transfer (image) file data over network?

转载 作者:行者123 更新时间:2023-11-28 08:27:25 26 4
gpt4 key购买 nike

我正在开发一个需要通过网络发送“文件名”、“文件大小”和文件数据的应用程序。我使用 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

最佳答案

将文件转储到网络上怎么样? HTTP 就是这样做的。事实上,我看到您使用的协议(protocol)与 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;
}
}

关于C++, boost : how to fill buffer and transfer (image) file data over network?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3416171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com