Basic asio example
I had a lot of trouble find a really basic example (hey the stuff I want to do is not that hard) for using the boost::asio library. This library is supposed to be the bee's knees and will be included in C++ TR2 (and is already included in Boost) so I thought it was worthwhile to fight with it for some time...
Here's a sample app adapted from the included blocking_tcp_echo_server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//
// blocking_tcp_echo_server.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2008 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 [url=http://www.boost.org/LICENSE_1_0.txt]http://www.boost.org/LICENSE_1_0.txt[/url])
//
#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include "asio.hpp"
using asio::ip::tcp;
const int max_read_length = 10;
const int max_response_length = 100;
typedef boost::shared_ptr<tcp::socket> socket_ptr;
void session(socket_ptr sock)
{
try
{
for (;;)
{
// Reading demonstration
char data[max_read_length];
asio::error_code error;
size_t length = sock->read_some(asio::buffer(data, max_read_length), error);
if (error == asio::error::eof)
{
std::cout << "Socket closed by peer" << std::endl;
break; // Connection closed cleanly by peer.
}
else if (error)
{
throw asio::system_error(error); // Some other error.
}
std::cout << "Read " << length << " bytes" << std::endl;
// Writing demonstration
char response[max_response_length];
sprintf((char*)response, "Read %d bytes dude it rocks\n", length);
length = asio::write(*sock, asio::buffer(response, strlen(response)), asio::transfer_all(), error);
std::cout << "Written " << length << " bytes " << std::endl;
if (error)
{
std::cout << "Error during writing: " << error.message() << std::endl;
return;
}
// uncomment this if you just want to echo
//asio::write(*sock, asio::buffer(data, length));
}
}
catch (std::exception& e)
{
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
void server(asio::io_service& io_service, short port)
{
tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
for (;;)
{
socket_ptr sock(new tcp::socket(io_service));
std::cout << "Blocking in accept" << std::endl;
a.accept(*sock);
std::cout << "Got a connection" << std::endl;
// uncomment this to multithread
// asio::thread t(boost::bind(session, sock));
// only need one simultaneous thread
session(sock);
}
}
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: blocking_tcp_echo_server <port>\n";
return 1;
}
asio::io_service io_service;
using namespace std; // For atoi.
server(io_service, atoi(argv[1]));
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
This post is licensed under CC BY 4.0 by the author.