boost-asio

Opening multiple ports using Boost Asio libraries

不打扰是莪最后的温柔 提交于 2019-12-05 22:03:56
I am a newbie for Boost Asio libraries, my requirement is to build a server, which should listen on 600 different ports asynchronously (TCP communication). Can someone suggest me a smart way to achieve this using Boost Asio. I have tried using echo server example provided at Boost Asio documentation but could not really understand much boost::asio::io_service io_service; using namespace std; // For atoi. for(long port=50000;port<=50600;port++) { server s(io_service, port); io_service.run(); } Can someone throw light on this? The io_service is responsible for handling all I/O that is assigned

Can I read from a socket synchronously using Boost.Asio with a timeout on a multithreaded I/O service?

僤鯓⒐⒋嵵緔 提交于 2019-12-05 21:49:49
问题 I have an application that uses Boost.Asio for TCP and UDP socket communications. I understand that the 'A' in "Asio" stands for Asynchronous , so the library is bent toward encouraging you to use asynchronous I/O when possible. I have a few cases where synchronous socket reads are preferable. At the same time, however, I would like to set a timeout on said receive calls, so there's no possibility of the read blocking indefinitely. This appears to be a pretty common problem among Boost.Asio

C++ Boost.ASIO async_read_until slow

▼魔方 西西 提交于 2019-12-05 21:26:49
问题 I'm having an unusual issue. I have a C++ Boost.ASIO web server, and to handle incoming requests I'm using this code: boost::asio::async_read_until( socket_, response_, "\r\n\r\n", boost::bind( &connection::handle_read_headers, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred ) ); (where "socket_" is my boost::asio::ip::tcp::socket and "response_" is a boost::asio::streambuf) I'm trying to just grab the headers of the request, then I later do

boost asio stateful server design

﹥>﹥吖頭↗ 提交于 2019-12-05 19:16:37
I am writing a Server application on boost::asio Here I instantiate a Server that owns an io_service Server is started by server.start() method which calls Server::accept() and that creates a new Session even before it gets a new connection Calls _acceptor.async_accept thats set a callback that calls Session::handler() method which does the handshaking. Now is it okay to create a socket even before getting a new client ? and in this code the Session is auto destructed after writing an "Hello " Message which is Okay in case of HTTP, But I want to carry on a Stateful Communication. so socket

My server exited with code 137

前提是你 提交于 2019-12-05 19:05:49
I wrote a C++ server/client pair using C++11, boost::asio and HDF5. The server was running fine for a some time (2 days), and then it stopped with code 137. Since I executed the server with an infinite loop, it was restarted. Unfortunately, my error logs don't provide sufficient information to understand the problem. So I've been trying to understand what this code means. It seems there's consensus that this means it's an error of 128+9 , with 9 meaning that the program was killed with kill -9 . Now I'm not sure at all why this happened. I need help to find out. By reading further, I found out

boost::asio set_option error

我们两清 提交于 2019-12-05 18:43:38
I have a simple boost::asio::ip::tcp::acceptor which does almost nothing - it accepts connections in an infinite loop. I then have a number of connectors running at the same time trying to connect... pSocket->async_connect(endpoint, [=](boost::system::error_code error) { if(!error) { boost::asio::ip::tcp::no_delay noDelay(true); pSocket->set_option(noDelay, error); assert(!error); std::cout << error.message() << '\n'; // "An invalid argument was supplied" } }); Everything is running in infinite loops and I'm running 2 clients and 1 server, all loopback connections. After a while (hundreds of

Fail to listen to UDP Port with boost::asio

与世无争的帅哥 提交于 2019-12-05 17:58:59
I have a server that gathers information and broadcasts some messages across the local network. I'm using boost::asio to broadcast these via UDP on port 8079 and I can verify with WireShark that these packets are actually broadcasted as intended. Now, naturally, I want to follow up with a listener that can react to these messages, but I am struggling to receive anything. My current approach is: boost::asio::io_service io_service; boost::asio::ip::udp::socket socket(io_service); boost::asio::ip::udp::endpoint local( boost::asio::ip::address::from_string("192.168.2.102"), 8079); boost::system:

Synchronous TCP Read in Node.js

我的梦境 提交于 2019-12-05 17:57:57
Is there a way to do a synchronous read of a TCP socket in node.js? I'm well aware of how to do it asynchronously by adding a callback to the socket's 'data' event: socket.on('data', function(data) { // now we have the string data to do whatever with }); I'm also aware that trying to block with a function call instead of registering callbacks goes against node's design, but we are trying to update an old node module that acts as a client for my university while maintaining backwards compatibility. So we currently have: var someData = ourModule.getData(); Where getData() previously had a bunch

How to write to a boost::asio::mutable_buffer?

时光毁灭记忆、已成空白 提交于 2019-12-05 16:17:32
I have some code that provides me with a pointer to a buffer, and the buffer's size that I need to fill with data. I represent this buffer with a boost::asio::mutable_buffer instance, but how do I properly use this buffer (e.g. write a string to it, ...) and have boost enforce the buffer boundaries? Here's some pseudo code: size_t some_callback(void *ptr, size_t) { // this function is called by 3rd party return our_handler(boost::asio::mutable_buffer(ptr, size)); } size_t our_handler(const boost::asio::mutable_buffer &buffer) { const std::string test("test"); // How do I write this string into

How game servers with Boost:Asio work asynchronously?

社会主义新天地 提交于 2019-12-05 14:30:15
I am trying to create a game server, and currently, I am making it with threads. Every object( a player , monster ), has its own thread with while(1) cycle , in witch particular functions are performed. And the server basically works like this: main(){ //some initialization while(1) { //reads clients packet //directs packet info to a particular object //object performs some functions //then server returns result packet back to client Sleep(1); } I have heard that is not efficient to make the server using threads like that, and I should consider to use Boost::Asio, and make the functions work