All pastes #477110 Raw Edit

chrome

public cpp v1 · immutable
·published 2007-05-08 13:56 UTC
rendered paste body
#include <iostream>#include <string>#include <sstream>#include <boost/asio.hpp>using namespace std;class TcpConnection{private:	class BadStreamException {};	public:	boost::asio::ip::tcp::iostream stream;	TcpConnection(string host, string port)	{		cout << "TcpConnection(string, string) constructor." << endl;		this->connect(host, port);	}	TcpConnection(string host, int port)	{		cout << "TcpConnection(string, int) constructor." << endl;			stringstream portStream;		portStream << port;		this->connect(host, portStream.str());	}	~TcpConnection()	{	}	void TcpConnection::connect(string host, string port)	{		cout << "host [" << host << "] port [" << port << "]" << endl;		try		{			boost::asio::io_service io_service;			boost::asio::ip::tcp::resolver resolver(io_service);			boost::asio::ip::tcp::resolver::query query(host, port);			stream.connect(query);		}		catch (boost::asio::error& e)		{			cout << "asio::error: " << e << endl;		}		catch (std::exception& e)		{			cout << "std::exception: " << e.what() << endl;		}	}		std::string TcpConnection::getline() 	{		string line;		if (stream.good())		{			std::getline(stream, line);			return line;		}		else		{			throw BadStreamException();		}	}};int main(int argc, char* argv[]){	string line;	TcpConnection connection("stupendous.net", 22);	line = connection.getline();	cout << "line [" << line << "]" << endl;	TcpConnection connection2("stupendous.net", "22");	line = connection2.getline();	cout << "line [" << line << "]" << endl;	return 0;}