boost-asio

Why in asio's example the tcp acceptor pattern uses shared_pointer model wrapping heap socket, while udp use stack socket?

大兔子大兔子 提交于 2021-01-28 20:50:50
问题 Source code: https://think-async.com/Asio/asio-1.18.0/doc/asio/tutorial/tutdaytime7/src.html tcp_server shows an intention to use socket on the heap, wrapped by a type called tcp_connection. class tcp_server { private: void start_accept() { tcp_connection::pointer new_connection = tcp_connection::create(io_context_); acceptor_.async_accept(new_connection->socket(), boost::bind(&tcp_server::handle_accept, this, new_connection, asio::placeholders::error)); } void handle_accept(tcp_connection:

Thread-safety when accessing data from N-theads in context of an async TCP-server

社会主义新天地 提交于 2021-01-28 19:39:06
问题 As the title says i have a question concerning the following scenario (simplyfied example): Assume that i have an object of the Generator-Class below, which continuously updates its dataChunk member ( running in the main thread). class Generator { void generateData(); uint8_t dataChunk[999]; } Furthermore i have an async. acceptor of TCP-connections to which 1-N clients can connect to (running in a second thread). The acceptor starts a new thread for each new client-connection, in which an

Thread-safety when accessing data from N-theads in context of an async TCP-server

南楼画角 提交于 2021-01-28 19:21:46
问题 As the title says i have a question concerning the following scenario (simplyfied example): Assume that i have an object of the Generator-Class below, which continuously updates its dataChunk member ( running in the main thread). class Generator { void generateData(); uint8_t dataChunk[999]; } Furthermore i have an async. acceptor of TCP-connections to which 1-N clients can connect to (running in a second thread). The acceptor starts a new thread for each new client-connection, in which an

Read only desired amount of bytes using Boost.Asio

巧了我就是萌 提交于 2021-01-28 14:26:51
问题 I'm just creating a very simple C++ class that provides me a few methods, like connect() and read() , instead of exposing all the Boost.Asio socket calls. Right now, I'm trying to find out how to create a method that reads only the desired amount of bytes: SocketClient::read(int bytes, char* data); //reads desired amount of bytes and puts them in data, size of data>bytes! Unfortunately, I found no read_byte function in Boost.Asio. I do not want to drop bytes that have been received, but not

Boost asio network disconnection handling

柔情痞子 提交于 2021-01-28 12:42:20
问题 I am using boost asio for my TCP Server, in this I am using async_read_some for reading . Application is working fine when network is connected, normal connection closing are handled correctly like (EOF,abrupt closing). But my problem is I am not getting any error when network cable is unplugged. socket is open , and I get error when I am writing on socket. This is the way socket work. Question: Can this is handled in Boost asio by any method? 回答1: You may want to set "keep alive" on the

How to use Zero-copy sendmsg/Receive in Boost.Asio [closed]

六月ゝ 毕业季﹏ 提交于 2021-01-28 11:21:46
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 12 months ago . Improve this question I am using Boost.Asio, I want to improve my system by using Zero-copy sendmsg/Receive. Can I use Zero-copy sendmsg/Receive in Boost.Asio? Could you give me how to use them if I can use them? 回答1: Short answer, you can only if your in-memory representation

Boost asio async_read_until followed by async_read

允我心安 提交于 2021-01-28 10:34:21
问题 I used the async tcp server example from boost which is near to what my application is doing. The code example below is a fully working example. At first I start an async read operation until the delimiter char. In this case it is the http header complete sequence. The request contains some payload which is "hello world" (11 bytes). For a simplified example I use lambda handlers here. The first handler is called wit a length of 148 which is the header including four bytes for the delimiter

Boost asio async_read_until followed by async_read

核能气质少年 提交于 2021-01-28 10:31:56
问题 I used the async tcp server example from boost which is near to what my application is doing. The code example below is a fully working example. At first I start an async read operation until the delimiter char. In this case it is the http header complete sequence. The request contains some payload which is "hello world" (11 bytes). For a simplified example I use lambda handlers here. The first handler is called wit a length of 148 which is the header including four bytes for the delimiter

Boost.Asio read_some: End of file error

做~自己de王妃 提交于 2021-01-28 04:00:06
问题 This example is based on Chapter 1 in book Boost.Asio C++ Network Programming book. I am attempting to build a simple client and synchronous server using Boost.Asio library. Here is the client code #define BOOST_ASIO_SEPARATE_COMPILATION #include <iostream> #include <boost/asio/impl/src.hpp> #include <boost/asio/ip/tcp.hpp> #include <boost/asio/write.hpp> #include <string> int main(int argc, char **argv) { boost::asio::io_service ioservice; boost::asio::ip::tcp::endpoint ep(boost::asio::ip:

How do I set TCP Keep Alive to a specific value using Boost ASIO?

空扰寡人 提交于 2021-01-28 03:03:39
问题 I know boost ASIO has a socket option to enable tcp keep-alive, but how can I set it to a specific value? If not through Boost defined types, perhaps I can get the socket handle and set the option using posix setsocketopt() call? 回答1: There are two parts to keep-alive. First, it can be enabled with default values. Second, keep alive interval and timeout can be set. For the first part you can use this: unsigned long val = 1; int res = setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, (char*)&val,