Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
488 views

Boost - Asio - Network Programming

The document discusses network programming with Boost.Asio. It introduces the key network I/O objects in Boost.Asio like tcp::acceptor, tcp::socket, udp::socket. It explains how these objects are used to accept connections, send and receive data over the network. It also discusses helpers like resolvers to map hostnames to IP addresses and best practices like error handling for asynchronous operations.

Uploaded by

NgocThangPham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
488 views

Boost - Asio - Network Programming

The document discusses network programming with Boost.Asio. It introduces the key network I/O objects in Boost.Asio like tcp::acceptor, tcp::socket, udp::socket. It explains how these objects are used to accept connections, send and receive data over the network. It also discusses helpers like resolvers to map hostnames to IP addresses and best practices like error handling for asynchronous operations.

Uploaded by

NgocThangPham
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Network programming

with Boost.Asio
What are the network I/O objects?
How are the network I/O objects used?
What are helper functions for async operations?
What are best practices?

Boris Schling, boris@highscore.de


Meeting C++, Dsseldorf, 10 November 2012

Network I/O objects


ip::tcp::acceptor

ip::udp::resolver

ip::tcp::iostream

ip::udp::socket

ip::tcp::resolver

ip::icmp::resolver

ip::tcp::socket

ip::icmp::socket

Network programming with Boost.Asio

Network I/O objects


ip::tcp::acceptor

ip::tcp::iostream

ip::tcp::resolver

ip::udp::resolver

ip::icmp::resolver

ip::tcp::socket

ip::udp::socket

ip::icmp::socket

Network programming with Boost.Asio

Acceptor

void handler(const boost::system::error_code &ec) {}


...
ip::tcp::endpoint endpoint(ip::tcp::v4(), 80);
ip::tcp::acceptor acceptor(io_service, endpoint);
acceptor.listen();
ip::tcp::socket sock(io_service);
acceptor.async_accept(sock, handler);
...

Use acceptors to accept incoming TCP/IP


connections
Network programming with Boost.Asio

Socket

void handler(const boost::system::error_code &ec,


std::size_t bytes_transferred) {}
...
ip::tcp::socket sock(io_service);
boost::array<char, 4096> buf;
sock.async_read_some(buffer(buf), handler);
async_read(sock, buffer(buf), handler);
...

Use sockets to receive data (consider using


free-standing helper functions)
Network programming with Boost.Asio

Socket

void handler(const boost::system::error_code &ec,


std::size_t bytes_transferred) {}
...
ip::tcp::socket sock(io_service);
std::string data = "Hello, world!";
sock.async_write_some(buffer(data), handler);
async_write(sock, buffer(data), handler);
...

Use sockets to send data (consider using


free-standing helper functions)
Network programming with Boost.Asio

Resolver

void handler(const boost::system::error_code &ec,


ip::tcp::resolver::iterator it)
{ sock.connect(*it); }
...
ip::tcp::resolver resolver(io_service);
ip::tcp::resolver::query query("www.highscore.de", "80");
resolver.async_resolve(query, handler);
...

Use resolvers to map hostnames and


services to IP addresses
Network programming with Boost.Asio

IOStream

ip::tcp::iostream s("www.highscore.de", "9999");


if (s)
{
s << "Hello, world!" << std::endl;
std::string data;
std::getline(s, data);
}

Use streams to send and receive data over


networks with the operators << and >>
Network programming with Boost.Asio

Best Practices
Always check the error code in handlers
Use free-standing helper functions for
connecting, sending and receiving
Use io_service::post() to execute code in an
event loops thread
Use boost::enable_shared_from_this for
dynamically allocated objects whose member
functions are used for callbacks

Network programming with Boost.Asio

More information
Boost.Asio documentation:
http://www.boost.org/libs/asio/

Boost.Asio examples:
http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/examples.html

Online book:
http://en.highscore.de/cpp/boost/asio.html
http://www.highscore.de/cpp/boost/asio.html (German)
http://zh.highscore.de/cpp/boost/asio.html (Chinese)

JTC1/SC22/WG21 paper N3389:


http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3389.pdf

Network programming with Boost.Asio

10

You might also like