Book Image

Boost.Asio C++ Network Programming

By : Wisnu Anggoro
Book Image

Boost.Asio C++ Network Programming

By: Wisnu Anggoro

Overview of this book

Table of Contents (15 chapters)
Boost.Asio C++ Network Programming Second Edition
Credits
About the Authors
Acknowledgements
About the Reviewers
www.PacktPub.com
Preface
Index

Developing a client and server program


We have already had the network wrapper code simplify our programming process in developing a network application by using the Boost.Asio library. Now, let us create a client and server program by using our wrapper code.

Creating a simple echo server

We are going to create a server program that will echo out all traffic it retrieves from the client. In this case, we will use the telnet as the client, as we've done previously. The file has to be saved as echoserver.cpp, and the content will look like the following:

/* echoserver.cpp */
#include "wrapper.h"
#include <conio.h>
#include <boost/thread/mutex.hpp>

boost::mutex global_stream_lock;

class MyConnection : public Connection {
private:
  void OnAccept(const std::string &host, uint16_t port) {
    global_stream_lock.lock();
    std::cout << "[OnAccept] " << host << ":" << port << "\n";
    global_stream_lock.unlock();

    Recv();
  }

  void OnConnect...