Commit 75687dd1 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading
+55 −0
Changes for 0298_01_Code/src/Accepting_a_connection.cpp: 55 added lines, 0 removed lines.
Original line number Diff line number Diff line
#include <boost/asio.hpp>
#include <iostream>

using namespace boost;

int main()
{
	// The size of the queue containing the pending connection
	// requests.
	const int BACKLOG_SIZE = 30;

	// Step 1. Here we assume that the server application has
	// already obtained the protocol port number.
	unsigned short port_num = 3333;

	// Step 2. Creating a server endpoint.
	asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(),
		port_num);

	asio::io_service ios;

	try {
		// Step 3. Instantiating and opening an acceptor socket.
		asio::ip::tcp::acceptor acceptor(ios, ep.protocol());

		// Step 4. Binding the acceptor socket to the 
		// server endpint.
		acceptor.bind(ep);

		// Step 5. Starting to listen to incoming connection
		// requests.
		acceptor.listen(BACKLOG_SIZE);

		// Step 6. Creating an active socket.
		asio::ip::tcp::socket sock(ios);

		// Step 7. Processing the next connection request and 
		// connecting the active socket to the client.
		acceptor.accept(sock);

		// At this point 'sock' socket is connected to 
		//the client application and can be used to send data to
		// or receive data from it.
	}
	catch (system::system_error &e) {
		std::cout << "Error occured! Error code = " << e.code()
			<< ". Message: " << e.what();

		return e.code().value();
	}

	return 0;
}

+37 −0
Changes for 0298_01_Code/src/Binding_a_socket_TCP_passive.cpp: 37 added lines, 0 removed lines.
Original line number Diff line number Diff line
#include <boost/asio.hpp>
#include <iostream>

using namespace boost;

int main()
{
	// Step 1. Here we assume that the server application has already 
	// obtained the protocol port number.
	unsigned short port_num = 3333;

	// Step 2. Creating an endpoint.
	asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(), port_num);

	// Used by 'acceptor' class constructor.
	asio::io_service ios;

	// Step 3. Creating and opening an acceptor socket.
	asio::ip::tcp::acceptor acceptor(ios, ep.protocol());

	boost::system::error_code ec;

	// Step 4. Binding the acceptor socket.
	acceptor.bind(ep, ec);

	// Handling errors if any.
	if (ec != 0) {
		// Failed to bind the acceptor socket. Breaking execution.
		std::cout << "Failed to bind the acceptor socket."
			<< "Error code = " << ec.value() << ". Message: "
			<< ec.message();

		return ec.value();
	}

	return 0;
}
+39 −0
Changes for 0298_01_Code/src/Binding_a_socket_UDP_active.cpp: 39 added lines, 0 removed lines.
Original line number Diff line number Diff line
#include <boost/asio.hpp>
#include <iostream>

using namespace boost;

int main()
{
	// Step 1. Here we assume that the server application has
	// already obtained the protocol port number.
	unsigned short port_num = 3333;

	// Step 2. Creating an endpoint.
	asio::ip::udp::endpoint ep(asio::ip::address_v4::any(),
		port_num);

	// Used by 'socket' class constructor.
	asio::io_service ios;

	// Step 3. Creating and opening a socket.
	asio::ip::udp::socket sock(ios, ep.protocol());

	boost::system::error_code ec;

	// Step 4. Binding the socket to an endpoint.
	sock.bind(ep, ec);

	// Handling errors if any.
	if (ec != 0) {
		// Failed to bind the socket. Breaking execution.
		std::cout << "Failed to bind the socket."
			<< "Error code = " << ec.value() << ". Message: "
			<< ec.message();

		return ec.value();
	}

	return 0;
}
+40 −0
Changes for 0298_01_Code/src/Connecting_a_socket.cpp: 40 added lines, 0 removed lines.
Original line number Diff line number Diff line
#include <boost/asio.hpp>
#include <iostream>

using namespace boost;

int main()
{
	// Step 1. Assume that the client application has already
	// obtained the IP address and protocol port number of the
	// target server.
	std::string raw_ip_address = "127.0.0.1";
	unsigned short port_num = 3333;

	try {
		// Step 2. Creating an endpoint designating 
		// a target server application.
		asio::ip::tcp::endpoint
			ep(asio::ip::address::from_string(raw_ip_address),
			port_num);

		asio::io_service ios;

		// Step 3. Creating and opening a socket.
		asio::ip::tcp::socket sock(ios, ep.protocol());

		// Step 4. Connecting a socket.
		sock.connect(ep);
	}
	// Overloads of asio::ip::address::from_string() and 
	// asio::ip::tcp::socket::connect() used here throw
	// exceptions in case of error condition.
	catch (system::system_error &e) {
		std::cout << "Error occured! Error code = " << e.code()
			<< ". Message: " << e.what();

		return e.code().value();
	}

	return 0;
}
+51 −0
Changes for 0298_01_Code/src/Connecting_a_socket_DNS_name.cpp: 51 added lines, 0 removed lines.
Original line number Diff line number Diff line
#include <boost/asio.hpp>
#include <iostream>

using namespace boost;

int main()
{
	// Step1. Assume that the client application has already
	// obtained the DNS name and protocol port number and
	// represented them as strings.
	std::string host = "samplehost.book";
	std::string port_num = "3333";

	// Used by a 'resolver' and a 'socket'.
	asio::io_service ios;

	// Creating a resolver's query.
	asio::ip::tcp::resolver::query resolver_query(host, port_num,
		asio::ip::tcp::resolver::query::numeric_service);

	// Creating a resolver.
	asio::ip::tcp::resolver resolver(ios);

	try {
		// Step 2. Resolving a DNS name.
		asio::ip::tcp::resolver::iterator it =
			resolver.resolve(resolver_query);

		// Step 3. Creating a socket.
		asio::ip::tcp::socket sock(ios);

		// Step 4. asio::connect() method iterates over
		// each endpoint until successfully connects to one
		// of them. It will throw an exception if it fails
		// to connect to all the endpoints or if other
		// error occurs.
		asio::connect(sock, it);
	}
	// Overloads of asio::ip::tcp::resolver::resolve and 
	// asio::connect() used here throw
	// exceptions in case of error condition.
	catch (system::system_error &e) {
		std::cout << "Error occured! Error code = " << e.code()
			<< ". Message: " << e.what();

		return e.code().value();
	}

	return 0;
}
Loading