$treeview $search $mathjax
00001 // ////////////////////////////////////////////////////////////////////// 00002 // Import section 00003 // ////////////////////////////////////////////////////////////////////// 00004 // STL 00005 #include <cassert> 00006 #include <iostream> 00007 #include <string> 00008 // Boost.ASIO 00009 #include <boost/asio.hpp> 00010 // Boost.Array 00011 #include <boost/array.hpp> 00012 00013 // /////////// M A I N //////////////// 00014 int main (int argc, char* argv[]) { 00015 00016 // Host name 00017 std::string lHostname = "localhost"; 00018 00019 // Service name (as specified within /etc/services) 00020 // The "aria" service corresponds to the port 2624 00021 const std::string lServiceName = "aria"; 00022 00023 try { 00024 00025 if (argc >= 2) { 00026 lHostname = argv[1]; 00027 } 00028 00029 boost::asio::io_service lIOService; 00030 00031 boost::asio::ip::tcp::resolver lResolver (lIOService); 00032 00033 boost::asio::ip::tcp::resolver::query lQuery (lHostname, lServiceName); 00034 00035 boost::asio::ip::tcp::resolver::iterator itEndPoint = 00036 lResolver.resolve (lQuery); 00037 boost::asio::ip::tcp::resolver::iterator lEnd; 00038 00039 boost::asio::ip::tcp::socket lSocket (lIOService); 00040 boost::system::error_code lError = boost::asio::error::host_not_found; 00041 00042 // 00043 while (lError && itEndPoint != lEnd) { 00044 const boost::asio::ip::tcp::endpoint lEndPoint = *itEndPoint; 00045 00046 // DEBUG 00047 std::cout << "Testing end point: " << std::endl; 00048 00049 lSocket.close(); 00050 lSocket.connect (lEndPoint, lError); 00051 ++itEndPoint; 00052 } 00053 00054 // 00055 if (lError) { 00056 throw boost::system::system_error (lError); 00057 } 00058 assert (!lError); 00059 00060 // DEBUG 00061 const boost::asio::ip::tcp::endpoint lValidEndPoint; 00062 std::cout << "Valid end point: " << lValidEndPoint << std::endl; 00063 00064 // Send a message to the server 00065 const std::string lMessage ("Hello AirInv Server!"); 00066 boost::asio::write (lSocket, boost::asio::buffer (lMessage), 00067 boost::asio::transfer_all(), lError); 00068 00069 // Read the reply from the server 00070 boost::array<char, 256> lBuffer; 00071 00072 size_t lLength = lSocket.read_some (boost::asio::buffer(lBuffer), lError); 00073 00074 // Some other error than connection closed cleanly by peer 00075 if (lError && lError != boost::asio::error::eof) { 00076 throw boost::system::system_error (lError); 00077 } 00078 00079 // DEBUG 00080 std::cout << "Reply from the server: "; 00081 std::cout.write (lBuffer.data(), lLength); 00082 std::cout << std::endl; 00083 00084 } catch (std::exception& lException) { 00085 std::cerr << lException.what() << std::endl; 00086 } 00087 00088 return 0; 00089 }