io_service::run() 永远不会返回
io_service::run() never returns
UPDATE_2我最终使用
if (socket.available())
如果套接字上有数据,我会读取它,如果没有,我会跳过。
更新我有一个问题。io_service::run(( 永远不会返回以下代码片段:
客户端:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
using namespace std;
using boost::asio::ip::tcp;
void handler(
const boost::system::error_code &error,
size_t bytes_transferred
)
{
cout << "Handler called" << endl;
}
int main()
{
//establishing connection
boost::asio::io_service io_service;
tcp::socket socket(io_service);
tcp::resolver resolver(io_service);
tcp::resolver::query query("localhost", "17073");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
boost::asio::connect(socket, endpoint_iterator);
//connection established
boost::array<char, 128> buf;
socket.async_read_some(boost::asio::buffer(buf), boost::bind(handler, _1, _2));
//async_read_some returns
io_service.run(); //I suppose that handler_for_async_response is called if:
//1) EOF was read (no data read twice) by async_read_some
//2) something was in fact read
//is this right?
//execution does not get here
return 0;
}
服务器端(只接受连接并挂在那里什么都不做(:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
using namespace std;
using boost::asio::ip::tcp;
int main()
{
//establishing connection
boost::asio::io_service io_service;
tcp::socket socket(io_service);
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 17073));
acceptor.accept(socket);
std::cout << "connection has been established!" << std::endl;
//connection established
while (true)
{
//do nothing
}
return 0;
}
我想做的是查看套接字上是否有任何数据。如果是 - 然后阅读它,如果不是 - 然后继续。
PS我打算从套接字读取和写入套接字。
无论如何,我是 boost::asio 的新手,我希望有人能告诉我我做错了什么。谢谢!
更新 用代码回答更新的问题:
客户端正在等待 128 字节或 EOF。这些都没有来,因为服务器不发送任何内容(但不关闭连接(。
任一更改
-
客户,例如
-
不要等待实际数据到达
boost::array<char, 0> buf;
-
或者在
deadline_timer
(左右(后使其socket.cancel()
。
-
-
服务器,例如用真正的套接字写入替换无限循环
下面是一个示例
住在科里鲁
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <iostream>
using boost::asio::ip::tcp;
using namespace std;
#ifdef SERVER
int main()
{
//establishing connection
boost::asio::io_service io_service;
tcp::socket socket(io_service);
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 17073));
acceptor.accept(socket);
std::cout << "connection has been established!" << std::endl;
//connection established
socket.send(boost::asio::buffer(std::string("Hello worldn")));
}
#else
void handler(
boost::array<char, 128> const& buf,
const boost::system::error_code &error,
size_t bytes_transferred
)
{
cout << "Handler called (" << error.message() << "): " << endl;
cout.write(buf.data(), bytes_transferred);
}
int main()
{
//establishing connection
boost::asio::io_service io_service;
tcp::socket socket(io_service);
tcp::resolver resolver(io_service);
tcp::resolver::query query("127.0.0.1", "17073");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
boost::asio::connect(socket, endpoint_iterator);
//connection established
boost::array<char, 128> buf;
socket.async_read_some(boost::asio::buffer(buf), boost::bind(handler, boost::ref(buf), _1, _2));
//async_read_some returns
io_service.run(); //I suppose that handler_for_async_response is called if:
//1) EOF was read (no data read twice) by async_read_some
//2) something was in fact read
//is this right?
//execution does not get here
return 0;
}
#endif
指纹:
//(server)
connection has been established!
//(client)
Handler called (Success):
世界您好
您必须对同步或异步编程下定决心。
在这种情况下,我认为您可能真的在寻找tcp::istream
。
运行在任务用完时返回:
- http://www.boost.org/doc/libs/1_59_0/doc/html/boost_asio/reference/io_service/run/overload1.html
run()
功能将一直阻止,直到所有工作完成并且没有更多处理程序要调度,或者直到io_service
停止为止。多个线程可以调用
run()
函数来设置线程池,io_service
可以从中执行处理程序。在池中等待的所有线程都是等效的,io_service
可以选择其中任何一个来调用处理程序。从
run()
函数正常退出意味着io_service
对象已停止(stop(( 函数返回 true(。对run()
、run_one()
、poll()
或poll_one()
的后续调用将立即返回,除非事先调用reset()
。
因此,您在服务上还有其他待处理的工作(io_service::工作?检查一下。将您的代码设为 SSCCE,我们将能够看到更多。
- 为什么是谷神星协方差.计算()似乎永远运行而不返回?
- cudaMemcpyToSymbol只是挂起,永远不会返回.GPU 处理速度为 100%.代码在 K40 上工作正常,但
- 无法从串行端口读取.WaitCommEvent() 永远不会返回
- 程序返回-1073741571而不是永远进行的值
- OpenRAVE ControllerBase 在 IsDone() 方法处阻塞并且永远不会返回
- LoadLibrary 永远不会在 Visual Studio 2013 c++ 项目中返回
- 如果发送进程停止,boost::interprocess::timed_received()永远不会返回
- CoCreateInstance() 永远不会返回
- 为什么这个对isspace()的调用永远不会返回true
- 对 Boost JSON 解析器的调用永远不会返回
- 为什么 ReadFile() 不返回 0 ?程序尝试永远从管道读取数据
- waveOutWrite 缓冲区永远不会返回到应用程序
- Poco C++ 1.4.3p1 的 DatagramSocket ReceiveBytes() 永远不会返回。我是否滥用了该功能?
- 为什么从 write() 调用 __kernel_vsyscall() 永远不会返回
- ICLRuntimeHost Start方法永远不会返回
- 为什么eof()永远不会返回true
- io_service::run() 永远不会返回
- 如何永远从"ACE_Reactor::instance()->run_reactor_event_loop();"街区返回?
- Winapi定时器回调线程,永远不会返回
- Boost read()永远不会返回,即使在服务器上执行写(ssl) Boost