boost-asio

boost asio write/read vector

江枫思渺然 提交于 2019-11-28 02:19:19
I have trouble reading a vector from boost asio buffer. I have this vector: std::vector<float> points; And I send it with boost asio write boost::asio::write (socket, boost::asio::buffer(&new_buffers->points.front(), nr_points * 3 * sizeof (float))); On the other end I have: std::vector<float> recv_vector; tcp_socket.async_read_some(boost::asio::buffer(recv_vector), read_handler); When I do recv_vector.size() , its always empty. Can somebody tell me what I am doing wrong? Marek Asio is not a serialization library. It does not serialize random vectors (you could use Boost Serialization for that

boost::asio read from /dev/input/event0

梦想的初衷 提交于 2019-11-28 01:46:37
问题 I am looking to use boost::asio to read from a 12 digit keypad. I currently can do it without boost, this way: fd = open ("/dev/input/event0", 0_NONBLOCK); read (fd, &ev, sizeof ev); Do you know how I could do this with boost::asio? I am using Linux and c++. This post and this post are useful. I would not use serial port port (io, "/dev/usb/hiddev0") because its not serial, right? Thank you. 回答1: On my system, event2 represents the mouse, and the following simple readloop program works like a

Reading JSON from a socket using boost::asio

泪湿孤枕 提交于 2019-11-28 01:22:55
问题 I am currently trying to transfer some JSON data over the network from a client to a server using the socket API of boost-asio. My client essentially does this: int from = 1, to = 2; boost::asio::streambuf buf; ostream str(&buf); str << "{" << "\"purpose\" : \"request\"" << "," << endl << "\"from\" : " << from << "," << endl << "\"to\" : " << to << "," << endl << "}" << endl; // Start an asynchronous operation to send the message. boost::asio::async_write(socket_, buf, boost::bind(&client:

Do boost asio sockets have proper RAII cleanup

喜你入骨 提交于 2019-11-28 00:44:23
问题 I tried looking through source but I cant navigate that much of a template code. Basically: this is what documentation says (for close() ): Remarks For portable behaviour with respect to graceful closure of a connected socket, call shutdown() before closing the socket. I can do that manually, but if possible it would be nice to rely on RAII. So if I have socket going out of scope do I need to call shutdown() and close() on it, or it will be done automatically? 回答1: One can rely on the socket

boost::asio async_read guarantee all bytes are read

隐身守侯 提交于 2019-11-28 00:13:09
I have a server that receives a compressed string (compressed with zlib) from a client, and I was using async_receive from the boost::asio library to receive this string, it turns out however that there is no guarantee that all bytes will be received, so I now have to change it to async_read . The problem I face is that the size of the bytes received is variable, so I am not sure how to use async_read without knowing the number of bytes to be received. With the async_receive I just have a boost::array<char, 1024> , however this is a buffer that is not necessarily filled completely. I wondered

Exception handling in Boost.Asio

一世执手 提交于 2019-11-27 23:50:32
问题 Boost.Asio documentation suggests the following exception handling pattern: boost::asio::io_service io_service; ... for (;;) { try { io_service.run(); break; // run() exited normally } catch (my_exception& e) { // Deal with exception as appropriate. } } The problem with it is that the context of exception is lost at the point when it's handled. For example, if I have multiple socket sessions in a given io_service, I don't know which one caused the exception. What would be a better way to

How to design proper release of a boost::asio socket or wrapper thereof

一曲冷凌霜 提交于 2019-11-27 23:17:56
I am making a few attempts at making my own simple asynch TCP server using boost::asio after not having touched it for several years. The latest example listing I can find is: http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/tutorial/tutdaytime3/src.html The problem I have with this example listing is that (I feel) it cheats and it cheats big, by making the tcp_connection a shared_ptr, such that it doesn't worry about the lifetime management of each connection. (I think) They do this for brevity, since it is a small tutorial, but that solution is not real world. What if you wanted to

Boost.Asio SSL thread safety

筅森魡賤 提交于 2019-11-27 23:13:18
Do I create one strand that all of my SSL sockets share, or one strand per SSL context (shared by any associated sockets)? Boost.Asio SSL documentation states this, but it doesn't mention contexts. I assume that this means I must use only one strand for everything, but I think this was written before OpenSSL had multithreading support. SSL and Threads SSL stream objects perform no locking of their own. Therefore, it is essential that all asynchronous SSL operations are performed in an implicit or explicit strand. Note that this means that no synchronisation is required (and so no locking

boost::asio UDP broadcasting

前提是你 提交于 2019-11-27 22:04:49
I want to broadcast UDP messages to all computers in a local network using boost::asio . Working through the examples I came up with try { socket.open(boost::asio::ip::udp::v4()); boost::asio::socket_base::broadcast option(true); socket.set_option(option); endpoint = boost::asio::ip::udp::endpoint( boost::asio::ip::address::from_string("192.168.1.255"), port); } catch(std::exception &e) { } and want to broadcast messages from my queue with while(!queue.empty()) { std::string message = queue.front(); boost::system::error_code ignored_error; socket.send_to( boost::asio::buffer(message), endpoint

Working with boost::asio::streambuf

半世苍凉 提交于 2019-11-27 21:00:52
问题 Looking for a boost::asio (and with himself boost) decided to write asynchronous server. To store incoming data I use boost::asio::streambuf. Here I have a problem. When I receive a second message from the client and subsequent I see that in the buffer contains a data from previous messages. Although I call Consume method at the input buffer. What's wrong? class tcp_connection // Using shared_ptr and enable_shared_from_this // because we want to keep the tcp_connection object alive // as long