Making an asynchronous Client with boost::asio

i'm trying to make an asynchronous Client with boost::asio, i use the daytime asynchronous Server(in the tutorial).

However sometimes the Client don't receive the Message, sometimes it do :O

I'm sorry if this is too much code, but i don't know what's wrong :/


#include <iostream> #include <stdio.h> #include <ostream> #include <boost/thread.hpp> #include <boost/bind.hpp> #include <boost/array.hpp> #include <boost/asio.hpp> using namespace std; using boost::asio::ip::tcp; class TCPClient { public: TCPClient(boost::asio::io_service& IO_Service, tcp::resolver::iterator EndPointIter); void Write(); void Close(); private: boost::asio::io_service& m_IOService; tcp::socket m_Socket; boost::array<char, 128> m_Buffer; size_t m_BufLen; private: void OnConnect(const boost::system::error_code& ErrorCode, tcp::resolver::iterator EndPointIter); void OnReceive(const boost::system::error_code& ErrorCode); void DoClose(); }; TCPClient::TCPClient(boost::asio::io_service& IO_Service, tcp::resolver::iterator EndPointIter) : m_IOService(IO_Service), m_Socket(IO_Service) { tcp::endpoint EndPoint = *EndPointIter; m_Socket.async_connect(EndPoint, boost::bind(&TCPClient::OnConnect, this, boost::asio::placeholders::error, ++EndPointIter)); } void TCPClient::Close() { boost::bind(&TCPClient::DoClose, this)); } void TCPClient::OnConnect(const boost::system::error_code& ErrorCode, tcp::resolver::iterator EndPointIter) { if (ErrorCode == 0) // Successful connected { m_Socket.async_receive(boost::asio::buffer(, m_BufLen), boost::bind(&TCPClient::OnReceive, this, boost::asio::placeholders::error)); } else if (EndPointIter != tcp::resolver::iterator()) { m_Socket.close(); tcp::endpoint EndPoint = *EndPointIter; m_Socket.async_connect(EndPoint, boost::bind(&TCPClient::OnConnect, this, boost::asio::placeholders::error, ++EndPointIter)); } } void TCPClient::OnReceive(const boost::system::error_code& ErrorCode) { if (ErrorCode == 0) { std::cout << << std::endl; m_Socket.async_receive(boost::asio::buffer(, m_BufLen), boost::bind(&TCPClient::OnReceive, this, boost::asio::placeholders::error)); } else { DoClose(); } } void TCPClient::DoClose() { m_Socket.close(); } int main() { try { boost::asio::io_service IO_Service; tcp::resolver Resolver(IO_Service); tcp::resolver::query Query("", "daytime"); tcp::resolver::iterator EndPointIterator = Resolver.resolve(Query); TCPClient Client(IO_Service, EndPointIterator); boost::thread ClientThread( boost::bind(&boost::asio::io_service::run, &IO_Service)); std::cout << "Client started." << std::endl; std::string Input; while (Input != "exit") { std::cin >> Input; } Client.Close(); ClientThread.join(); } catch (std::exception& e) { std::cerr << e.what() << std::endl; } }


-------------Problems Reply------------

Bother to init m_BufLen?

Category:boost Views:1 Time:2010-04-08

Related post

  • Flushing all asynchronous handlers in boost::asio 2011-04-15

    I'm running some tests that need asynchronous communication, and the underlying framework is Asio. Sometimes, a handler is kept in the processing loop even if the test has been tore down, for good reasons. But then, it is called after the targets are

  • Fast data (image) transfer server client using Boost Asio 2011-07-25

    I'm relatively new to network programming and have some questions on best practices for continuous fast data (image) transfer between a client and a server using Boost Asio. Important point, we cannot apply compression that lowers image quality. We u

  • Handler requirement for Asynchronous Operation in boost::asio 2011-12-19

    It is specified in boost::asio document that a handler for async_accept() must satisfy the following function signature: void accept_handler( const boost::system::error_code& ec) { ... } However, in the Daytime.3 example, using boost::bind, the h

  • bad characters with asynchrone function in boost asio 2011-11-16

    i have a problem with data transfert in boost::asio. with the following code : Server.cpp void Server::accept(void) { Network::ptr connection = Network::create(this->my_acceptor.get_io_service()); this->my_acceptor.async_accept(connection->g

  • Why my C++ Boost ASIO HTTP Client Returning Incomplete Response? 2011-11-12

    I'm new to C++ and I'm just trying to implement a simple HTTP Client using Boost ASIO Sync Http Client; I copied the example from Boost's site, just modified it to return the response as string rather than writing to console. My code is making a call

  • How should I delete a child object from within a parent's slot? Possibly boost::asio specific 2010-12-17

    I have written a network server class that maintains a std::set of network clients. The network clients emit a signal to the network server on disconnect (via boost::bind). When a network client disconnects, the client instance needs to be removed fr

  • boost asio async_read_until: how to raise error from a custom MatchCondition? 2011-01-31

    I am trying to optimize an application layer protocol that has a mixed mode protocol ( line protocol for meta info, handshake, acknowledgement, etc. and binary for data ). In a state where the client is waiting for an ACK, I do async_read_until(socke

  • Routine when starting boost::asio::io_service 2011-05-16

    I want to create an http client using boost asio. To have a structured and optimized I have looked into the examples of boost asio to have some idea of what a good implementation should look like. Mostly, I have followed the structure of HTTP Server,

  • how to implemet POSIX select() based behaviour, within boost::asio 2009-05-26

    I've already wasted two days reading documentation of boost::asio And I still don't know how could I implement blocking select() like function for several sockets using only one thread (using boost framework). Asynchronous functions of boost::asio re

  • boost asio: 0 byte write 2009-07-06

    I need some help understanding a peculiar issue I'm having when using asio. I've a client -server app, with a c++ client(using boost asio) sending 2 byte hearbeat (say, every second) to a server(written in java) (and receiving lots of data as well).

  • How to send raw binary data using boost::asio 2011-07-28

    I'm writing a TCP client using boost::asio. I want to send an array of floats in their binary representation. Does boost provide a nice way to convert data to their binary representation for placement it in a boost::array or something? I have used Qt

  • Closing boost::asio::serial_port with pending read_async_some 2012-03-12

    I am chaining read_async_some() calls to asynchronously read from a serial port. At some point, I need to cancel the asynchronous reads using and detect this fact in the associated handlers. From the documentation for cancel(), I expected to do so si

  • Boost asio: io service is blocked during destruction step more some handler trouble 2012-03-13

    To manage tcp communication i'm using boost asio version 1.48 with x64 project setting on visual studio 10 with sdk 7.1 and windows 7. I designed a class with inner members: asio IO Service. asio tcp ipv4 Socket. asio Strand. a list of received Messa

  • Boost Asio asynchronous: Server sent message to client 2015-02-18

    I am working on a Server/Client application. Client send a message (header+body) to the Server. The server receives the message, make some modifications and sent it back. So the client sends the message. async_write (m_socket,boost::asio::buffer(msg

  • How to create a simple server/client application using boost.asio? 2010-05-03

    I was going over the examples of boost.asio and I am wondering why there isn't an example of a simple server/client example that prints a string on the server and then returns a response to the client. I tried to modify the echo server but I can't re

  • Why doesn't this Boost ASIO code work with this python client? 2010-08-26

    This code is identical to the original udp async echo server, but with a different socket. The response is transmitted and showing in wireshark, but then an ICMP Port Unreachable error is sent back to the server. I'm trying to understand why because

  • Boost asio : how to keep a client connection alive? 2010-11-28

    I try to create a client who keep the connection alive on the server. However, when i receive data once, the connection was closed. I don't understand why. I think that i should make a loop, but we told me that wasn't a good idea. class client { publ

  • Problem with Boost Asio asynchronous connection using C++ in Windows 2011-02-23

    Using MS Visual Studio 2008 C++ for Windows 32 (XP brand), I try to construct a POP3 client managed from a modeless dialog box. Te first step is create a persistent object -say pop3- with all that Boost.asio stuff to do asynchronous connections, in t

  • libboost ASIO. Simple asynchronous client server 2011-08-04

    I'm trying to implement a simple client/server in ASIO. I'd like the following on the serverside: onConnect() onDisconnect() onMessageRecieved(char* data) sendMessage(char* data) and on the client side: onConnect() onDisconnect() onMessageRecieved(ch

Copyright (C), All Rights Reserved.

processed in 0.201 (s). 11 q(s)