Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, here's a somewhat functional example to get you started. It forwards between two connections. Note that this simple example won't work for a web browser, as the client will attempt to make several connections, and this example only listens on one. Using this as a (very simple) base, you should be able to make some progress.</p> <p>The interesting stuff happens in <code>handle_read</code>, which is the callback that is executed when data is received. This function forwards the data between sockets. Notice that when we originally called it for the "local" and "remote" connections that the order we passed the sockets in is reversed (<code>read_from</code> and <code>write_to</code>).</p> <pre><code>#include &lt;iostream&gt; using namespace std; #include &lt;boost/asio.hpp&gt; #include &lt;boost/lexical_cast.hpp&gt; #include &lt;boost/thread.hpp&gt; #include &lt;boost/bind.hpp&gt; boost::asio::io_service&amp; io_service() { static boost::asio::io_service svc; return svc; } char local_data[1024] = {0}; char remote_data[1024] = {0}; void handle_read( boost::asio::ip::tcp::socket&amp; read_from, boost::asio::ip::tcp::socket&amp; write_to, char* read_buffer, size_t bytes, const boost::system::error_code&amp; e) { // this function is called whenever data is received // for debugging purposes, show the data in the console window // or write to file, or whatever... std::string data(read_buffer, read_buffer + bytes); std::cout &lt;&lt; data &lt;&lt; "\n"; // forward the received data on to "the other side" write_to.send( boost::asio::buffer(read_buffer, bytes)); // read more data from "this side" read_from.async_read_some( boost::asio::buffer(read_buffer, 1024), boost::bind(handle_read, boost::ref(read_from), boost::ref(write_to), read_buffer, boost::asio::placeholders::bytes_transferred, boost::asio::placeholders::error)); } int main(int argc, char** argv) { if(argc == 5) { boost::asio::io_service::work w(io_service()); boost::thread t(boost::bind(&amp;boost::asio::io_service::run, (&amp;io_service()))); // extract the connection information from the command line boost::asio::ip::address local_address = boost::asio::ip::address::from_string(argv[1]); uint16_t local_port = boost::lexical_cast&lt;uint16_t&gt;(argv[2]); boost::asio::ip::address remote_address = boost::asio::ip::address::from_string(argv[3]); uint16_t remote_port = boost::lexical_cast&lt;uint16_t&gt;(argv[4]); boost::asio::ip::tcp::endpoint local_ep(local_address, local_port); boost::asio::ip::tcp::endpoint remote_ep(remote_address, remote_port); // start listening on the "local" socket -- note this does not // have to be local, you could in theory forward through a remote device // it's called "local" in the logical sense boost::asio::ip::tcp::acceptor listen(io_service(), local_ep); boost::asio::ip::tcp::socket local_socket(io_service()); listen.accept(local_socket); // open the remote connection boost::asio::ip::tcp::socket remote_socket(io_service()); remote_socket.open(remote_ep.protocol()); remote_socket.connect(remote_ep); // start listening for data on the "local" connection local_socket.async_receive( boost::asio::buffer(local_data, 1024), boost::bind(handle_read, boost::ref(local_socket), boost::ref(remote_socket), local_data, boost::asio::placeholders::bytes_transferred, boost::asio::placeholders::error)); // also listen for data on the "remote" connection remote_socket.async_receive( boost::asio::buffer(remote_data, 1024), boost::bind(handle_read, boost::ref(remote_socket), boost::ref(local_socket), remote_data, boost::asio::placeholders::bytes_transferred, boost::asio::placeholders::error)); t.join(); } else { cout &lt;&lt; "proxy &lt;local ip&gt; &lt;port&gt; &lt;remote ip&gt; &lt;port&gt;\n"; } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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