boost-asio

How to asynchronously read to std::string using Boost::asio?

一个人想着一个人 提交于 2019-11-28 06:33:37
I'm learning Boost::asio and all that async stuff. How can I asynchronously read to variable user_ of type std::string? Boost::asio::buffer(user_) works only with async_write() , but not with async_read() . It works with vector, so what is the reason for it not to work with string? Is there another way to do that besides declaring char user_[max_len] and using Boost::asio::buffer(user_, max_len) ? Also, what's the point of inheriting from boost::enable_shared_from_this<Connection> and using shared_from_this() instead of this in async_read() and async_write() ? I've seen that a lot in the

How do I cleanly reconnect a boost::socket following a disconnect?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 06:21:31
My client application uses a boost::asio::ip::tcp::socket to connect to a remote server. If the app loses connection to this server (e.g. due to the server crashing or being shutdown) I would like it to attempt a re-connect at regular intervals until it succeeds. What do I need to do on the client-side to cleanly handle a disconnect, tidy up and then repeatedly attempt reconnects? Currently the interesting bits of my code look something like this. I connect like this: bool MyClient::myconnect() { bool isConnected = false; // Attempt connection socket.connect(server_endpoint, errorcode); if

C++ Boost ASIO: how to read/write with a timeout?

旧街凉风 提交于 2019-11-28 05:53:43
From reading other Stack Overflow entries and the boost::asio documentation, I've confirmed that there is no synchronous ASIO read/write calls that also provide an easy-to-use timeout as a parameter to the call. I'm in the middle of converting an old-school Linux socket application with select(2) calls that employs timeouts, and I need to do more-or-less the same. So what is the best way to do this in boost::asio ? Looking at the asio documentation, there are many confusing examples of various things to do with timers, but I'm quite confused. I'd love to see a simple-to-read example of this:

How do I make the boost/asio library repeat a timer?

我们两清 提交于 2019-11-28 05:50:54
问题 Here is the Code given on the Boost library documentation. #include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> void print(const boost::system::error_code& /*e*/) { std::cout << "Hello, world!\n"; } int main() { boost::asio::io_service io; boost::asio::deadline_timer t(io, boost::posix_time::seconds(5)); t.async_wait(print); io.run(); return 0; } Now when I run the above program it just waits for 5 seconds and then prints Hello World and stop. I

boost::io_service How to guarantee handler execution sequence

冷暖自知 提交于 2019-11-28 05:27:36
问题 I have a thread pool with boost::io_service on top. I use it for different CPU-bound tasks in whole application. For some tasks, I have to guarantee that tasks will be executed in specified order (decoding video stream). Using io_service::strand guaranties that tasks will not be executed currently, but it has no guarantee about the order of execution. In other words, task #5 may be executed before task #4. Is there any method to solve that problem, other than scheduling next task after

How do I perform a nonblocking read using asio?

喜夏-厌秋 提交于 2019-11-28 05:06:40
I am attempting to use boost::asio to read and write from a device on a serial port. Both boost::asio:read() and boost::asio::serial_port::read_some() block when there is nothing to read. Instead I would like to detect this condition and write a command to the port to kick-start the device. How can I either detect that no data is available? If necessary I can do everything asynchronously, I would just rather avoid the extra complexity if I can. You have a couple of options, actually. You can either use the serial port's built-in async_read_some function, or you can use the stand-alone function

HTTPS request with Boost.Asio and OpenSSL

那年仲夏 提交于 2019-11-28 05:05:53
I'm trying to read the ticker symbol at https://mtgox.com/api/0/data/ticker.php from my C++ application. I use Boost.Asio and OpenSSL because the service requires HTTPS. Boost version: 1.47.0 OpenSSL: 1.0.0d [8 Feb 2011] Win32 For the application; I took the example from http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/example/ssl/client.cpp to get started and modified it as follows: This is where I want to connect to: boost::asio::ip::tcp::resolver::query query("mtgox.com", "443"); I set verification to none because the handshake fails otherwise. I'm not sure if this is a problem with

How to create a boost ssl iostream?

佐手、 提交于 2019-11-28 04:23:36
I'm adding HTTPS support to code that does input and output using boost tcp::iostream (acting as an HTTP server). I've found examples (and have a working toy HTTPS server) that do SSL input/output using boost::asio::read/boost::asio::write, but none that use iostreams and the << >> operators. How do I turn an ssl::stream into an iostream? Working code: #include <boost/asio.hpp> #include <boost/asio/ssl.hpp> #include <boost/foreach.hpp> #include <iostream> #include <sstream> #include <string> using namespace std; using namespace boost; using boost::asio::ip::tcp; typedef boost::asio::ssl:

How to avoid data race with `asio::ip::tcp::iostream`?

非 Y 不嫁゛ 提交于 2019-11-28 04:04:23
问题 My question How do I avoid a data race when using two threads to send and receive over an asio::ip::tcp::iostream ? Design I am writing a program that uses an asio::ip::tcp::iostream for input and output. The program accepts commands from the (remote) user over port 5555 and sends messages over that same TCP connection to the user. Because these events (commands received from the user or messages sent to the user) occur asynchronously, I have separate transmit and receive threads. In this toy

How to integrate Boost.Asio main loop in GUI framework like Qt4 or GTK

喜夏-厌秋 提交于 2019-11-28 03:50:39
Is there any way to integrate Boost.Asio with Qt4 (preferred) or GTK main loop? GTK provides poll(2) like API so technically is should be possible. Qt provides its own networking layer, however I prefer to use existing code written for Boost.Asio. I want to integrate them without using an additional thread. Is there any reference how to do this for Qt4 (preferred) or GTKmm? Thanks. Edit I want to clearify several things to make the answer easier. Both Qt and GTKmm provide "select like" functionality: http://qt-project.org/doc/qt-5.0/qtcore/qsocketnotifier.html http://www.gtkmm.org/docs/glibmm