我找到了this question,它询问如何异步读取输入,但只适用于POSIX流描述符,这在Windows上不起作用。所以,我发现this tutorial这表明,而不是使用POSIX流描述符,我可以使用boost::asio::windows::stream_handle
。如何在Windows中使用boost asio异步读取命令行输入?
下面两个例子中,我想出了下面的代码。当我运行它时,我无法在命令提示符中输入任何内容,因为程序立即终止。我希望它能够捕获来自用户的任何输入,可能是std::string
,同时允许我的程序中的其他逻辑执行(即从Windows控制台执行异步I/O)。
本质上讲,我试图避免挡住了我的程序时,它试图从stdin
阅读。我不知道这是否可以在Windows中使用,因为我还发现了this post,它详细描述了另一个用户在尝试做同样的事情时遇到的问题。
#define _WIN32_WINNT 0x0501
#define INPUT_BUFFER_LENGTH 512
#include <cstdio>
#include <iostream>
#define BOOST_THREAD_USE_LIB // For MinGW 4.5 - (https://svn.boost.org/trac/boost/ticket/4878)
#include <boost/bind.hpp>
#include <boost/asio.hpp>
class Example {
public:
Example(boost::asio::io_service& io_service)
: input_buffer(INPUT_BUFFER_LENGTH), input_handle(io_service)
{
// Read a line of input.
boost::asio::async_read_until(input_handle, input_buffer, "\r\n",
boost::bind(&Example::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handle_read(const boost::system::error_code& error, std::size_t length);
void handle_write(const boost::system::error_code& error);
private:
boost::asio::streambuf input_buffer;
boost::asio::windows::stream_handle input_handle;
};
void Example::handle_read(const boost::system::error_code& error, std::size_t length)
{
if (!error)
{
// Remove newline from input.
input_buffer.consume(1);
input_buffer.commit(length - 1);
std::istream is(&input_buffer);
std::string s;
is >> s;
std::cout << s << std::endl;
boost::asio::async_read_until(input_handle, input_buffer, "\r\n",
boost::bind(&Example::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else if(error == boost::asio::error::not_found)
{
std::cout << "Did not receive ending character!" << std::endl;
}
}
void Example::handle_write(const boost::system::error_code& error)
{
if (!error)
{
// Read a line of input.
boost::asio::async_read_until(input_handle, input_buffer, "\r\n",
boost::bind(&Example::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
}
int main(int argc, char ** argv)
{
try {
boost::asio::io_service io_service;
Example obj(io_service);
io_service.run();
} catch(std::exception & e)
{
std::cout << e.what() << std::endl;
}
std::cout << "Program has ended" << std::endl;
getchar();
return 0;
}
我不是一个Windows用户,但是不会使用\ r \ n作为新的线路指示器吗? –
是的,但会阻止它的工作,因为\ n仍然在换行符序列中?我将分隔符字符串更改为“\ r \ n”,结果相同。 – nickb
哪里可以调用io_service :: run()? –