Network Programming With Boost
Network Programming With Boost
Network Programming With Boost
Jeff Garland
jeff@crystalclearsoftware.com
serialization
bind
regex
smart_ptr
date-time
Copyright© 2006-2008 CrystalClear Software
2008-May-09 Network Programming with Boost 3
Session Agenda
Introduction
Boost.asio Overview
Basic Synchronous Client Program
Boost.asio in depth
Asynchronous Server Program
Boost.serialization – short overview
Pitfalls of Network Programming
Network Programming Tools
Resources and Conclusion
Copyright© 2006-2008 CrystalClear Software
2008-May-09 Network Programming with Boost 4
What’s the need?
Many modern applications are now distributed
applications
Networking Advancements
Ubiquitous networks - opens possibility of making more
apps networked
‘Fast’ networks
Gigabit ethernet
Wireless N
Protocol Development
Continued advancements in ‘application protocols’
(example RSS)
protocol
port
80
«process» {http/tcp/ip} «process»
: Web Browser : Web Server
{transient} {persistent}
operating
system process Communication
path
<<external component>>
<<component>> 100 -trade ticker Exchange Feed System
Stock Server {Message Channel/udp}
<<component>>
100 - trade ticker
Stock Trade
{Message Channel/udp}
History
<<RDB>>
Trade
History
Connection-oriented Protocol
TCP/IP
TCP - Transmission Control Protocol
SCTP/IP
SCTP – Stream Control Transmission Protocol
Easy to learn
Network concepts based on Berkley sockets
2008-May-09
Portable Copyright© 2006-2008 CrystalClear Software
Network Programming with Boost 15
asio 101 - Platforms
Win32 with VC++ 7.1 and 8.x
Win32 with Borland C++ builder
Win32 with MinGW or Cygwin / g++
Linux with g++ 3.3 or later
Solaris with g++3.3 or later
Mac OS X 10.4 using g++ 3.3 or later
FreeBSD with g++3.3 or later
QNX Neutrino 6.3 with g++ 3.3. or later
Service Address
209.131.36.158:80
Client Computer Server Computer
return 0;
}
//eg: www.boost.org
string host_name = "localhost";
tcp::resolver resolver(io_service);
tcp::resolver::query query(host_name, "http");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
// To be expanded
while (endpoint_iterator != end)
if (connect_error)
throw connect_error;
//To Be expanded
boost::asio::streambuf request;
std::ostream request_stream(&request);
//http protocol stuff…
request_stream << "GET " << page_path << " HTTP/1.0\r\n";
request_stream << "Host: " << host_name << "\r\n";
request_stream << "Accept: */*\r\n";
request_stream << "Connection: close\r\n\r\n";
//To Be expanded
//To Be expanded
template<typename Sync_Read_Stream,
typename Mutable_Buffers>
std::size_t boost::asio::read (Sync_Read_Stream &s,
const Mutable_Buffers &buffers)
callback
Function objects
using boost::posix_time::seconds;
using boost::posix_time::milliseconds;
// Construct a timer without setting an expiration time.
boost::asio::deadline_timer timer(io_service);
Client Computer2
«process»
: Chat Client {chat/tcp/ip}
{transient}
.
.
. {chat/tcp/ip}
Client Computer3
«process»
: Chat Client
{transient}
chat_session
deliver(chat_msg)
chat_session
asio::tcp::acceptor asio::tcp::io_service deliver(chat_msg)
asio::tcp::socket
class chat_server
{
public:
chat_server(boost::asio::io_service& io_service,
const tcp::endpoint& endpoint)
: io_service_(io_service),
acceptor_(io_service, endpoint)
{
chat_session_ptr new_session(new chat_session(io_service_, room_));
acceptor_.async_accept(new_session->socket(),
boost::bind(&chat_server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}
class chat_room {
//…
void deliver(const chat_message& msg)
{
//…
std::for_each(participants_.begin(), participants_.end(),
boost::bind(&chat_participant::deliver, _1, boost::ref(msg)));
}
chat_server_list servers;
for (int i = 1; i < argc; ++i)
{
using namespace std; // For atoi.
tcp::endpoint endpoint(tcp::v4(), atoi(argv[i]));
chat_server_ptr server(new chat_server(io_service, endpoint));
servers.push_back(server);
}
io_service.run();
}
//catch, exit, etc
Copyright© 2006-2008 CrystalClear Software
2008-May-09 Network Programming with Boost
Notes on Chat Concurrency
Single threaded
Will support many active clients
Callback functions are ‘quick’
Excellent example of how to build many
basic servers
«subsystem»
Message
Protocol
«subsystem»
Client
Message 1 Message 3
Message 2
«subsystem»
Server
class MyMessageType {
vector<int> vi;
date d;
string s;
friend class boost::serialization::access;
template<class Archive>
serialize(Archive& ar, const unsigned int version) const
{
ar & s;
ar & vi;
ar & d; //d has custom serialization method
}
public:
//constructors and useful functions
};
Copyright© 2006-2008 CrystalClear Software
2008-May-09 Network Programming with Boost
serialization 101 – serialization
for custom types
template<class Archive>
serialize(Archive& ar, MyMessageType m &
const unsigned int version) const
{
ar & m.get_vi();
ar & m.get_d();
ar & m.get_s();
}
if (type == trade) {
StockTrade st;
ar >> st;
handle_trade(st);
else if (type == quote) {
StockQuote sq;
ar >> sq;
handle_quote(sq);
}
MyHandler
Message m; StockTrade
string symbol
StockQuote
Messaging
Application level message sequencing
Servers may require retransmit facilities
<<external component>>
<<component>> 100 -trade ticker Exchange Feed System
Stock Server {Message Channel/udp}
<<component>>
100 - trade ticker
Stock Trade
{Message Channel/udp}
History
<<RDB>>
Trade
History
Facility Boundary
1 5 ms 9 ms 2 ms f1.www.vip.sp1.yahoo.com [209.131.36.158]
2 29 ms 14 ms 14 ms 10.118.128.1
3 24 ms 10 ms 7 ms ip68-2-6-110.ph.ph.cox.net [68.2.6.110]
4 16 ms 15 ms 13 ms 68.2.13.94
5 13 ms 17 ms 17 ms 68.2.13.9
6 12 ms 32 ms 89 ms 68.2.13.5
7 10 ms 13 ms 29 ms 68.2.13.1
8 15 ms 13 ms 27 ms chnddsrj02-ae2.0.rd.ph.cox.net [68.2.14.5]
9 32 ms 43 ms 32 ms langbbr01-ae0.r2.la.cox.net [68.1.0.232]
10 30 ms 25 ms 31 ms exchange-cust1.la1.equinix.net [206.223.123.16]
11 40 ms * 38 ms ge-1-3-4-p142.pat1.pao.yahoo.com [216.115.96.42]
12 38 ms 42 ms 40 ms g-1-0-0-p150.msr2.sp1.yahoo.com [216.115.107.77]
13 41 ms 38 ms 42 ms UNKNOWN-209-131-32-23.yahoo.com [209.131.32.23]
14 45 ms 41 ms 43 ms f1.www.vip.sp1.yahoo.com [209.131.36.158]
NETSTAT [-a] [-b] [-e] [-n] [-o] [-p proto] [-r] [-s] [-v] [interval]
Active Connections