我在发送更大的文件然后几个字节(即700字节)时遇到问题 由于某种原因,我可以下载一个较小的文件,但当我试图下载任何更大的文件,它告诉我在我的浏览器(Chrome) - 下载已停止 (即251/700 B - 停止) 看起来它只下载251个字节然后停止
这是我的代码: Web_Connection.h
#include <string>
#include <fstream>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
class web_connection : public boost::enable_shared_from_this<web_connection>
{
public:
typedef boost::shared_ptr<web_connection> pointer;
static pointer create(boost::asio::io_service& io_service)
{
return pointer(new web_connection(io_service));
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
std::filebuf *pbuf;
std::ifstream sourcestr;
long size;
char * buffer;
sourcestr.open("file-copy.exe",std::ios::in | std::ios::binary);
// get pointer to associated buffer object
pbuf=sourcestr.rdbuf();
// get file size using buffer's members
size=pbuf->pubseekoff (0,std::ios::end,std::ios::in);
pbuf->pubseekpos (0,std::ios::in);
// allocate memory to contain file data
buffer=new char[size];
// get file data
pbuf->sgetn (buffer,size);
// download file http headers
std::stringstream stream;
stream << "HTTP/1.0 200 OK\r\n"
"Content-Description: File Transfer\r\n"
"Content-Type: application/octet-stream\r\n"
"Content-Transfer-Encoding: binary\r\n"
"Content-Disposition: attachment; filename=file.exe\r\n"
"Expires: 0\r\n"
"Cache-Control: must-revalidate, post-check=0, pre-check=0'\r\n"
"Pragma: public\r\n"
"Content-Length: " << size << "\r\n\r\n";
message_ = stream.str();
message_.append(buffer, size);
/*message_ = "HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n\r\n";*/
boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&web_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
private:
web_connection(boost::asio::io_service& io_service): socket_(io_service)
{}
void handle_write(const boost::system::error_code& error,size_t bytes_transferred)
{
}
tcp::socket socket_;
std::string message_;
};
Web Server.h
#include "web_connection.h"
class web_server
{
public:
web_server(unsigned short port)
{
try
{
acceptor_ = new tcp::acceptor(io_service, tcp::endpoint(tcp::v4(), port));
start_accept();
io_service.run();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
~web_server()
{
delete acceptor_;
}
private:
void start_accept()
{
web_connection::pointer new_connection = web_connection::create(acceptor_->get_io_service());
acceptor_->async_accept(new_connection->socket(),
boost::bind(&web_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
}
void handle_accept(web_connection::pointer new_connection, const boost::system::error_code& error)
{
if (!error)
{
new_connection->start();
start_accept();
}
}
boost::asio::io_service io_service;
tcp::acceptor* acceptor_;
};
EDITED: 我设法通过设置标题来解决它:
stream << "HTTP/1.0 200 OK\r\n"
"Content-Description: File Transfer\r\n"
"Content-Type: application/octet-stream\r\n"
"Content-Transfer-Encoding: binary\r\n"
"Content-Disposition: attachment; filename=example.exe\r\n"
"Expires: 0\r\n"
"Cache-Control: must-revalidate, post-check=0, pre-check=0'\r\n"
"Pragma: public\r\n"
"Content-Length: " << size << "\r\n\r\n";
因为我的目标是下载exe文件..
但由于某些原因,访问http://127.0.0.1:8080并不总是下载文件 有时候它会向我显示同样的错误 上次错误: asyc_write引发的错误代码:10054类别:系统 在我的浏览器上: (Chrome:Example.exe 536 / 542kb - Stoppd)
但是当我刷新页面几次时它最终会起作用:S 我怎么能让它始终工作?