async_write()
을 사용하여 소켓을 통해 클라이언트에 jpeg 프레임을 쓰려고합니다. 부스트 asynchronous TCP daytime server 예제를 시작점으로 사용했습니다.boost :: asio :: async_write, 65536 바이트보다 큰 데이터 쓰기.
#include <ctime>
#include <iostream>
#include <string>
#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;
std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}
class tcp_connection
: public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service)
{
return pointer(new tcp_connection(io_service));
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
message_ = make_daytime_string();
boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
private:
tcp_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_;
};
class tcp_server
{
public:
tcp_server(boost::asio::io_service& io_service)
: acceptor_(io_service, tcp::endpoint(tcp::v4(), 13))
{
start_accept();
}
private:
void start_accept()
{
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.io_service());
acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept, this, new_connection,
boost::asio::placeholders::error));
}
void handle_accept(tcp_connection::pointer new_connection,
const boost::system::error_code& error)
{
if (!error)
{
new_connection->start();
start_accept();
}
}
tcp::acceptor acceptor_;
};
int main()
{
try
{
boost::asio::io_service io_service;
tcp_server server(io_service);
io_service.run();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
나는 다음과 같이 async_write()
을 수행하는 방법을 수정 한 :
void start()
{
// fileToVector method reads contents of file to vector;
std::vector<unsigned char> message_ = fileToVector("/tmp/test");
boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&tcp_connection::handle_write, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
클라이언트를 사용하여 서버에서 큰 파일을 읽는, 서버는 65536 바이트의 최대 쓰기됩니다. boost::asio::async_write()
호출을 동기 호출 boost::asio::write()
으로 바꾸면 정확한 바이트 수가 클라이언트로 전송됩니다.
그래서 내 질문이 있는데, 어떻게을 사용하여 65536 바이트 이상을 보낼 수 있습니까? 어떤 도움이라도 대단히 감사하겠습니다.
또한 transmit_file ASIO의 예제 [link] (http://www.boost.org/doc/libs/1_48_0/doc/html/boost_asio/example/windows/transmit_file.cpp)에주의를 기울이십시오. 리눅스에서'TransmitFile' 윈도우 함수 나'sendfile'을 사용하면 버퍼 할당 오버 헤드를 피할 수 있습니다. – megabyte1024
문제는 설명한대로 정확하게, async_write가 완료되기 전에 버퍼가 파괴되었습니다. 답장과 답변을 보내 주셔서 감사합니다. –