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