boost-asio

can you set SO_RCVTIMEO and SO_SNDTIMEO socket options in boost asio?

与世无争的帅哥 提交于 2019-11-28 10:23:23
can you set SO_RCVTIMEO and SO_SNDTIMEO socket options in boost asio? If so how? Note I know you can use timers instead, but I'd like to know about these socket options in particular. Absolutely! Boost ASIO allows you to access the native/underlying data, which in this case is the SOCKET itself. So, let's say you have: boost::asio::ip::tcp::socket my_socket; And let's say you've already called open or bind or some member function that actually makes my_socket usable. Then, to get the underlying SOCKET value, call: SOCKET native_sock = my_socket.native(); int result = SOCKET_ERROR; if (INVALID

Using boost::asio::async_read with stdin?

北城以北 提交于 2019-11-28 10:22:45
short question: I have a realtime-simulation which is running as a backround process and is connected with pipes to the calling pogramm. I want to send commands to that process using stdin to get certain information from it via stdout. Now because it is a real-time process, it has to be a non blocking input. Is boost::asio::async_read in conjunction with iostream::cin a good idea for this task? how would I use that function if it is feasible? Any more suggestions? Sam Miller Look at boost::asio::posix::stream_descriptor http://www.boost.org/doc/libs/release/doc/html/boost_asio/example/cpp03

What's the difference between boost::io_service poll_one and run_one?

我的梦境 提交于 2019-11-28 10:12:57
io_service::poll_one Run the io_service object's event processing loop to execute one ready handler. vs io_service::run_one Run the io_service object's event processing loop to execute at most one handler. From that explanation it would seem poll_one could execute more than one handler? Does run_one or poll_one use any thread that's called run() or only the thread that calls poll_one/run_one? The documentation for ASIO is very sparse. poll_one will return immediately (non-blocking) in case there is no event to process. run_one will block the calling thread until one event is ready to process.

How to simulate boost::asio::write with a timeout

隐身守侯 提交于 2019-11-28 10:12:43
问题 I am trying to simulate boost::asio::write with timeout. Or you can say, I am trying to use boost::asio::async_write with a timeout. As I see, boost::asio::write blocks until all data has been written & read on the other side. This kind of functionality certainly requires a timeout. So, Reading through this simple answer here by Robert Hegner which demostrates how to do a boost::asio::async_read with timeout , I am trying adapt the same logic for write by doing so: size_t write_data_with_time

When do I have to use boost::asio:strand

こ雲淡風輕ζ 提交于 2019-11-28 09:23:55
Reading the document of boost::asio, it is still not clear when I need to use asio::strand. Suppose that I have one thread using io_service is it then safe to write on a socket as follows ? void Connection::write(boost::shared_ptr<string> msg) { _io_service.post(boost::bind(&Connection::_do_write,this,msg)); } void Connection::_do_write(boost::shared_ptr<string> msg) { if(_write_in_progress) { _msg_queue.push_back(msg); } else { _write_in_progress=true; boost::asio::async_write(_socket, boost::asio::buffer(*(msg.get())), boost::bind(&Connection::_handle_write,this, boost::asio::placeholders:

Linker error when compiling boost.asio example

穿精又带淫゛_ 提交于 2019-11-28 09:12:21
I'm trying to learn a little bit C++ and Boost.Asio. I'm trying to compile the following code example: #include <iostream> #include <boost/array.hpp> #include <boost/asio.hpp> using boost::asio::ip::tcp; int main(int argc, char* argv[]) { try { if (argc != 2) { std::cerr << "Usage: client <host>" << std::endl; return 1; } boost::asio::io_service io_service; tcp::resolver resolver(io_service); tcp::resolver::query query(argv[1], "daytime"); tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); tcp::resolver::iterator end; tcp::socket socket(io_service); boost::system::error_code

How can I store a boost::bind object as a class member?

戏子无情 提交于 2019-11-28 08:38:18
问题 I'm writing an application that uses boost::asio . Asio's async_receive (or async_read ) is invariably shown using a boost::bind object given for callback: boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.data(), chat_message::header_length), boost::bind(&chat_session::handle_read_header, shared_from_this(), boost::asio::placeholders::error)); That's perfectly nice, but I'd like not to have to recreate the bind object after each call to the callback. Instead, I'd like to create

How resume the execution of a stackful coroutine in the context of its strand?

不打扰是莪最后的温柔 提交于 2019-11-28 07:52:48
using Yield = asio::yield_context; using boost::system::error_code; int Func(Yield yield) { error_code ec; asio::detail::async_result_init<Yield, void(error_code, int)> init(yield[ec]); std::thread th(std::bind(Process, init.handler)); int result = init.result.get(); // <--- yield at here return result; } How to implement Process so that Func will resumed in the context of the strand that Func was originally spawned on? Boost.Asio uses a helper function, asio_handler_invoke , to provide a customization point for invocation strategies. For example, when a Handler has been wrapped by a strand ,

In Boost ASIO how can I set the source IP address to impersonate another server's IP address?

可紊 提交于 2019-11-28 07:03:58
问题 I have a Boost ASIO-based C++ server program and I'd like to be able to set the source IP address used by TCP to that of another server. I know one can read the source and destination IP addresses but presumably they can be set as well? Presumably if I set the "wrong" source IP address in the C++ code there will be some interaction with the network stack. Won't the network stack re-set the source IP address on the way out even if the C++ code is right? Is the right way to do this to write C++

boost::asio with boost::unique_future

自作多情 提交于 2019-11-28 07:02:45
According to http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/overview/cpp2011/futures.html , we can use boost::asio with std::future . But I couldn't find any information about working with boost::unique_future , which has more functions, such as then() . How can I use? Boost.Asio only provides first-class support for asynchronous operations to return a C++11 std::future or an actual value in stackful coroutines . Nevertheless, the requirements on asynchronous operations documents how to customize the return type for other types, such as Boost.Thread's boost::unique_future . It