Note that there are some explanatory texts on larger screens.

plurals
  1. POBoost.asio server-client. Connect between 2 computers
    text
    copied!<p>Im following the tutorials at the boost official web site <a href="http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/tutorial/tutdaytime1.html" rel="nofollow">http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/tutorial/tutdaytime1.html</a>.</p> <p>The program is working perfectly if i connect to "localhost" or "127.0.0.1" on the same machine. But if i run the client on another computer with the same network it fails to connect to the server. Why is this happening? and what would i have to do to get the client to run on another network?</p> <p>Error: connect: No connection could be made because the target machine actively refused it.</p> <p>Client:</p> <pre><code>#include &lt;iostream&gt; #include &lt;boost/array.hpp&gt; #include &lt;boost/asio.hpp&gt; using boost::asio::ip::tcp; int main() { try { boost::asio::io_service io_service; tcp::resolver resolver(io_service); char* serverName = "localhost"; tcp::resolver::query query(serverName, "daytime"); tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); tcp::socket socket(io_service); while(true) { boost::asio::connect(socket, endpoint_iterator); for (;;) { boost::array&lt;char, 128&gt; buf; boost::system::error_code error; size_t len = socket.read_some(boost::asio::buffer(buf), error); if (error == boost::asio::error::eof) break; // Connection closed cleanly by peer. else if (error) throw boost::system::system_error(error); // Some other error. std::cout.write(buf.data(), len); std::cout &lt;&lt;"\n"; } } } catch (std::exception&amp; e) { std::cerr &lt;&lt; e.what() &lt;&lt; std::endl; } return 0; } </code></pre> <p>Server:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;boost/asio.hpp&gt; using boost::asio::ip::tcp; int main() { try { boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13)); for (;;) { tcp::socket socket(io_service); acceptor.accept(socket); std::string message = "This is the Server!"; boost::system::error_code ignored_error; boost::asio::write(socket, boost::asio::buffer(message), ignored_error); } } catch (std::exception&amp; e) { std::cerr &lt;&lt; e.what() &lt;&lt; std::endl; } return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload