好的,我对套接字非常陌生,我正在尝试使用多个同步客户端来创建异步服务器。问题是,我的服务器无法从客户端读取任何数据,而且也没有错误!
这是我的服务器类:
#define READ_BUF_SIZE 512
struct Connection {
boost::asio::ip::tcp::socket socket;
boost::asio::streambuf read_buffer;
Connection(boost::asio::io_service & io_service) : socket(io_service), read_buffer() { }
Connection(boost::asio::io_service & io_service, size_t max_buffer_size) : socket(io_service), read_buffer(max_buffer_size) { }
};
class CServer {
boost::asio::io_service m_ioservice;
boost::asio::ip::tcp::acceptor m_acceptor;
std::list<Connection> m_connections;
using con_handle_t = std::list<Connection>::iterator;
public:
CServer() : m_ioservice(), m_acceptor(m_ioservice), m_connections() { }
void handle_read(con_handle_t con_handle, boost::system::error_code const & err, size_t bytes_transfered) {
if (bytes_transfered > 0) {
std::istream is(&con_handle->read_buffer);
std::string line;
std::getline(is, line);
std::cout << "Message Received: " << line << std::endl;
}
if (!err) {
do_async_read(con_handle);
}
else {
std::cerr << "Error on read: " << err.message() << std::endl;
m_connections.erase(con_handle);
}
}
void do_async_read(con_handle_t con_handle) {
auto handler = boost::bind(&CServer::handle_read, this, con_handle, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred);
boost::asio::async_read(con_handle->socket, con_handle->read_buffer, boost::asio::transfer_exactly(READ_BUF_SIZE), handler);
}
void handle_write(con_handle_t con_handle, std::shared_ptr<std::string> msg_buffer, boost::system::error_code const & err) {
if (!err) {
std::cout << "Finished sending message\n";
if (con_handle->socket.is_open()) {
// Write completed successfully and connection is open
}
}
else {
std::cerr << "Error on write: " << err.message() << std::endl;
m_connections.erase(con_handle);
}
}
void handle_accept(con_handle_t con_handle, boost::system::error_code const & err) {
if (!err) {
std::cout << "Connection from: " << con_handle->socket.remote_endpoint().address().to_string() << "\n";
std::cout << "Sending message\n";
auto buff = std::make_shared<std::string>("Hello World!\r\n\r\n");
auto handler = boost::bind(&CServer::handle_write, this, con_handle, buff, boost::asio::placeholders::error);
boost::asio::async_write(con_handle->socket, boost::asio::buffer(*buff), handler);
do_async_read(con_handle);
}
else {
std::cerr << "We had an error: " << err.message() << std::endl;
m_connections.erase(con_handle);
}
start_accept();
}
void start_accept() {
auto con_handle = m_connections.emplace(m_connections.begin(), m_ioservice);
auto handler = boost::bind(&CServer::handle_accept, this, con_handle, boost::asio::placeholders::error);
m_acceptor.async_accept(con_handle->socket, handler);
}
void listen(uint16_t port) {
auto endpoint = boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port);
m_acceptor.open(endpoint.protocol());
m_acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
m_acceptor.bind(endpoint);
m_acceptor.listen();
start_accept();
}
void run() {
m_ioservice.run();
}
};
该类首先通过构造函数创建,然后首先使用其上的listen
函数,然后使用其run
指令进行调用。
“世界,你好!”发送测试消息 ,服务器未从客户端收到任何信息(handle_read()
未得到调用)
Client::Client() : io_context(), resolver(io_context), endpoints(resolver.resolve("localhost", "daytime")), socket(io_context)
{
try
{
boost::asio::connect(socket, endpoints);
boost::array<unsigned char, PACKET_LENGTH> buf;
boost::system::error_code error;
socket.read_some(boost::asio::buffer(buf), error);
std::cout << "Got message." << std::endl;
boost::asio::write(socket, boost::asio::buffer("test message"), error);
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
最后,这是我的客户课程。它包含一个io_context
对象,一个套接字,一个解析器以及一个名为tcp::resolver::results_type
的{{1}}对象类型。
debug“ Got message”条目实际上已输出到控制台,因此服务器可以写入客户端,而“ test message”条目实际上在服务器中从未见过,大概是因为它无法读取。
问题出在哪里?预先感谢。
答案 0 :(得分:1)
问题在这里
boost::asio::async_read(con_handle->socket, con_handle->read_buffer,
boost::asio::transfer_exactly(READ_BUF_SIZE), handler);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ where READ_BUF_SIZE is 512
当恰好读取了512个字节或客户端的套接字被破坏或socket.shutdown()
禁用了其发送操作时,将调用您的处理程序。
因此,重点是客户端代码中Client
对象的生存期。如果您的客户代码如下:
int main() {
Client c; // send data, delete object, socket is destroyed
return 0;
}
服务器handle_read
中的用error_code == boost::asio::error::eof (end of file or stream)
调用,这意味着客户端中的套接字已被破坏。但是您可以检查streambuf的内容,其中包含“测试消息”。
但是如果您客户的代码如下:
int main() {
Client c;
// wait here for X seconds
return 0;
}
在服务器端,直到X秒钟过去,您都看不到任何输出。您可以使用
boost::asio::write(socket, boost::asio::buffer("test message"), error);
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send); // <---
shutdown
使end_of_file
被发送到服务器,读取操作被中止,handle_read
的处理程序被调用,您可以检查error_code是否为{{ 1}},如果是这样,则可以打印strambuf的内容。
如果您不知道发送了多少字节,则可以将error::eof
函数与定界符一起使用以指示消息的结尾(在此示例中为换行符):
async_read_until()