Thread: Understanding Async Code

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    139

    Understanding Async Code

    Hello,

    I found an example online but I am unsure how to READ the incoming data async. I do not see a function for it. How can I add this to the code?


    Here is the code I am talking about


    Source listing for Daytime.3 - 1.55.0


    Code:
    //
    // server.cpp
    // ~~~~~~~~~~
    //
    // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
    //
    // Distributed under the Boost Software License, Version 1.0. (See accompanying
    // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
    //
    
    #include <ctime>
    #include <iostream>
    #include <string>
    #include <boost/bind.hpp>
    #include <boost/shared_ptr.hpp>
    #include <boost/enable_shared_from_this.hpp>
    #include <boost/asio.hpp>
    
    using boost::asio::ip::tcp;
    
    std::string make_daytime_string()
    {
      using namespace std; // For time_t, time and ctime;
      time_t now = time(0);
      return ctime(&now);
    }
    
    class tcp_connection
      : public boost::enable_shared_from_this<tcp_connection>
    {
    public:
      typedef boost::shared_ptr<tcp_connection> pointer;
    
      static pointer create(boost::asio::io_service& io_service)
      {
        return pointer(new tcp_connection(io_service));
      }
    
      tcp::socket& socket()
      {
        return socket_;
      }
    
      void start()
      {
        message_ = make_daytime_string();
    
        boost::asio::async_write(socket_, boost::asio::buffer(message_),
            boost::bind(&tcp_connection::handle_write, shared_from_this(),
              boost::asio::placeholders::error,
              boost::asio::placeholders::bytes_transferred));
      }
    
    private:
      tcp_connection(boost::asio::io_service& io_service)
        : socket_(io_service)
      {
      }
    
      void handle_write(const boost::system::error_code& /*error*/,
          size_t /*bytes_transferred*/)
      {
      }
    
      tcp::socket socket_;
      std::string message_;
    };
    
    class tcp_server
    {
    public:
      tcp_server(boost::asio::io_service& io_service)
        : acceptor_(io_service, tcp::endpoint(tcp::v4(), 13))
      {
        start_accept();
      }
    
    private:
      void start_accept()
      {
        tcp_connection::pointer new_connection =
          tcp_connection::create(acceptor_.get_io_service());
    
        acceptor_.async_accept(new_connection->socket(),
            boost::bind(&tcp_server::handle_accept, this, new_connection,
              boost::asio::placeholders::error));
      }
    
      void handle_accept(tcp_connection::pointer new_connection,
          const boost::system::error_code& error)
      {
        if (!error)
        {
          new_connection->start();
        }
    
        start_accept();
      }
    
      tcp::acceptor acceptor_;
    };
    
    int main()
    {
      try
      {
        boost::asio::io_service io_service;
        tcp_server server(io_service);
        io_service.run();
      }
      catch (std::exception& e)
      {
        std::cerr << e.what() << std::endl;
      }
    
      return 0;
    }
    
    
    
    Thanks in advance

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    I cant do 2 way communication with the client/server?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by EverydayDiesel View Post
    I cant do 2 way communication with the client/server?
    Yes you can, but that's not how a daytime server/client works.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    I dont really care about the daytime server/client functionality as much as I want a client / server that are communicating with each other.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    C++11 Examples - 1.60.0

    Look at the tcp echo server/client examples.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Was networking factored into the STL for C++17?

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    https://isocpp.org/std/status

    Looks like it's still a TS (Technical Specification).
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    thank you for all the responses.

    I am trying to convert this code to my version of c++ in this example doc/html/boost_asio/example/cpp11/echo/async_tcp_echo_server.cpp - 1.60.0

    Code:
    $ gcc -vUsing built-in specs.
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
    Target: x86_64-linux-gnu
    Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.4-2ubuntu1~14.04.1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
    Thread model: posix
    gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.1)
    The error I am getting is ...

    Code:
    std::make_shared<session>(std::move(socket_))->start();
    this is the error
    Code:
    include/server.h|31|error: ‘move’ is not a member of ‘std’|
    How can convert this code to work with my version of gcc?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use g++, not gcc and be sure to compiled with the flag -std=c++0x, -std=c++11, -std=c++14 or -std=c++1y. And upgrade your compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads vs async events
    By zacs7 in forum Tech Board
    Replies: 4
    Last Post: 02-10-2010, 10:29 PM
  2. async socket issues
    By ZerOrDie in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2003, 06:10 PM
  3. Error on Async socket startup!
    By SyntaxBubble in forum Windows Programming
    Replies: 4
    Last Post: 03-16-2002, 07:36 PM
  4. Johnnie! Q about Async Sockets!(PLEASE READ!)
    By SyntaxBubble in forum Windows Programming
    Replies: 0
    Last Post: 03-15-2002, 03:07 PM
  5. Async Socket Examples?
    By SyntaxBubble in forum Windows Programming
    Replies: 1
    Last Post: 01-04-2002, 06:08 PM