<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Ping Pong Server mit boost::asio]]></title><description><![CDATA[<p>Hallo allerseits,</p>
<p>ich versuche mich gerade an einem Server der sich zu seines Gleichen verbindet um dann ein Paket hin und her zu senden.</p>
<p>Meine boost-Version ist 1.46.1.</p>
<p>Anbei habe ich einen ersten Entwurf der sich stark an <a href="http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/example/http/client/async_client.cpp" rel="nofollow">http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/example/http/client/async_client.cpp</a> orientiert.</p>
<p>Der Client macht nichts 'dolles, er zieht genau eine Nachricht von einem Server, der in meinem Fall <a href="http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/tutorial/tutdaytime3/src.html" rel="nofollow">http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/tutorial/tutdaytime3/src.html</a> ist. Dieser Server ist nur ein Behelf bis mein Client selbst ein Server geworden ist und sich selbst Pakete zuschicken kann.</p>
<p>Meine erste Frage: Wie verändere ich meinen Client so das dieser ständig Nachricht nach Nachricht zieht? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/307817/ping-pong-server-mit-boost-asio</link><generator>RSS for Node</generator><lastBuildDate>Thu, 06 Aug 2026 09:31:21 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/307817.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 06 Sep 2012 07:54:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Ping Pong Server mit boost::asio on Thu, 06 Sep 2012 07:54:52 GMT]]></title><description><![CDATA[<p>Hallo allerseits,</p>
<p>ich versuche mich gerade an einem Server der sich zu seines Gleichen verbindet um dann ein Paket hin und her zu senden.</p>
<p>Meine boost-Version ist 1.46.1.</p>
<p>Anbei habe ich einen ersten Entwurf der sich stark an <a href="http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/example/http/client/async_client.cpp" rel="nofollow">http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/example/http/client/async_client.cpp</a> orientiert.</p>
<p>Der Client macht nichts 'dolles, er zieht genau eine Nachricht von einem Server, der in meinem Fall <a href="http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/tutorial/tutdaytime3/src.html" rel="nofollow">http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/tutorial/tutdaytime3/src.html</a> ist. Dieser Server ist nur ein Behelf bis mein Client selbst ein Server geworden ist und sich selbst Pakete zuschicken kann.</p>
<p>Meine erste Frage: Wie verändere ich meinen Client so das dieser ständig Nachricht nach Nachricht zieht? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2248953</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248953</guid><dc:creator><![CDATA[goran]]></dc:creator><pubDate>Thu, 06 Sep 2012 07:54:52 GMT</pubDate></item><item><title><![CDATA[Reply to Ping Pong Server mit boost::asio on Thu, 06 Sep 2012 08:02:41 GMT]]></title><description><![CDATA[<p>goran schrieb:</p>
<blockquote>
<p>Meine erste Frage: Wie verändere ich meinen Client so das dieser ständig Nachricht nach Nachricht zieht? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
</blockquote>
<p>Indem du<br />
a) das &quot;Nachrichtenziehen&quot; in eine Schleife packst, oder<br />
b) nach ASIO-Manier den passenden Completion-Handler so implementierst, dass nach dem Empfang einer Nachricht gleich nach der nächsten gefragt wird.</p>
<p>Ich nehme mal an, dass das Ganze asynchron läuft (hab jetzt nicht die Zeit, das Tutorial anzuschauen), daher fällt a) wohl aus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2248958</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248958</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Thu, 06 Sep 2012 08:02:41 GMT</pubDate></item><item><title><![CDATA[Reply to Ping Pong Server mit boost::asio on Thu, 06 Sep 2012 08:06:51 GMT]]></title><description><![CDATA[<p>Entschuldigung, ich hatte meinen Code vergessen zu posten:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;istream&gt;
#include &lt;ostream&gt;
#include &lt;string&gt;
#include &lt;boost/asio.hpp&gt;
#include &lt;boost/bind.hpp&gt;

using boost::asio::ip::tcp;

class client
{
public:
  client(boost::asio::io_service&amp; io_service,
      const std::string&amp; server, const std::string&amp; port)
    : resolver_(io_service),
      socket_(io_service)
  {
    // Start an asynchronous resolve to translate the server and service names
    // into a list of endpoints.
    tcp::resolver::query query(server, port);
    resolver_.async_resolve(query,
        boost::bind(&amp;client::handle_resolve, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::iterator));
  }

private:
  void handle_resolve(const boost::system::error_code&amp; err,
      tcp::resolver::iterator endpoint_iterator)
  {
    if (!err)
    {
      // Attempt a connection to each endpoint in the list until we
      // successfully establish a connection.
      tcp::endpoint endpoint = *endpoint_iterator;
      socket_.async_connect(endpoint,
          boost::bind(&amp;client::handle_connect, this,
            boost::asio::placeholders::error));
    }
    else
    {
      std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; err.message() &lt;&lt; &quot;\n&quot;;
    }
  }

  void handle_connect(const boost::system::error_code&amp; err)
  {
    if (!err)
    {
      // The connection was successful. Read the response.
      boost::asio::async_read(socket_, response_,
          boost::bind(&amp;client::handle_response, this));
    }
    else if (err != boost::asio::error::eof)
    {
      std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; err.message() &lt;&lt; &quot;\n&quot;;
    }
  }

  void handle_response()
  {
    if (response_.size() &gt; 0)
      std::cout &lt;&lt; &quot;Response: &quot; &lt;&lt; &amp;response_;
    else
      std::cout &lt;&lt; &quot;Response is empty.&quot; &lt;&lt; std::endl;
  }

  tcp::resolver resolver_;
  tcp::socket socket_;
  boost::asio::streambuf request_;
  boost::asio::streambuf response_;
};

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 3)
    {
      std::cout &lt;&lt; &quot;Usage: async_client &lt;server&gt; &lt;port&gt;\n&quot;;
      std::cout &lt;&lt; &quot;Example:\n&quot;;
      std::cout &lt;&lt; &quot;  async_client localhost 2020\n&quot;;
      return 1;
    }

    boost::asio::io_service io_service;
    client c(io_service, argv[1], argv[2]);
    io_service.run();
  }
  catch (std::exception&amp; e)
  {
    std::cout &lt;&lt; &quot;Exception: &quot; &lt;&lt; e.what() &lt;&lt; &quot;\n&quot;;
  }

  return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2248960</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248960</guid><dc:creator><![CDATA[goran]]></dc:creator><pubDate>Thu, 06 Sep 2012 08:06:51 GMT</pubDate></item><item><title><![CDATA[Reply to Ping Pong Server mit boost::asio on Thu, 06 Sep 2012 08:10:05 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>b) nach ASIO-Manier den passenden Completion-Handler so implementierst, dass nach dem Empfang einer Nachricht gleich nach der nächsten gefragt wird.</p>
</blockquote>
<p>Geht das gut, beliebig viele Handler hintereinander aufzurufen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2248964</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248964</guid><dc:creator><![CDATA[goran]]></dc:creator><pubDate>Thu, 06 Sep 2012 08:10:05 GMT</pubDate></item><item><title><![CDATA[Reply to Ping Pong Server mit boost::asio on Thu, 06 Sep 2012 08:16:35 GMT]]></title><description><![CDATA[<p>Ich habe handle_connect() aus meinem Bsp. um einen weiteren async_read() erweitert:</p>
<pre><code class="language-cpp">void handle_connect(const boost::system::error_code&amp; err)
  {
    if (!err)
    {
      // The connection was successful. Read the response.
      boost::asio::async_read(socket_, response_,
          boost::bind(&amp;client::handle_response, this));
      boost::asio::async_read(socket_, response_,
          boost::bind(&amp;client::handle_response, this));
    }
    else if (err != boost::asio::error::eof)
    {
      std::cout &lt;&lt; &quot;Error: &quot; &lt;&lt; err.message() &lt;&lt; &quot;\n&quot;;
    }
  }
</code></pre>
<p>Die Ausgabe (mittels handle_response()) ist dann:</p>
<pre><code>Response: Thu Sep  6 10:11:59 2012
Response is empty.
</code></pre>
<p>Das zweite Lesen ging daneben. Was habe ich falsch gemacht? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2248965</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248965</guid><dc:creator><![CDATA[goran]]></dc:creator><pubDate>Thu, 06 Sep 2012 08:16:35 GMT</pubDate></item><item><title><![CDATA[Reply to Ping Pong Server mit boost::asio on Thu, 06 Sep 2012 08:16:50 GMT]]></title><description><![CDATA[<p>goran schrieb:</p>
<blockquote>
<p>Entschuldigung, ich hatte meinen Code vergessen zu posten</p>
</blockquote>
<p>Ändert nichts an der Antwort <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /><br />
Der Completion-Handler den du ändern willst, ist dann wohl handle_response.</p>
<p>PS: Wenn du einen C++11-fähigen Compiler hast, bieten sich an Stelle von boost::bind Lambdas an:</p>
<pre><code class="language-cpp">using namespace boost::system;
using namespace boost::asio;

class client
{
public:
  client(io_service&amp; io_service,
      const std::string&amp; server, const std::string&amp; port)
    : resolver_(io_service),
      socket_(io_service)
  {
    // Start an asynchronous resolve to translate the server and service names
    // into a list of endpoints.
    tcp::resolver::query query(server, port);
    resolver_.async_resolve(query, 
      [this](const boost::system::error_code&amp; err, tcp::resolver::iterator endpoint_iterator)
      {
         //... Inhalt von handle_connect...
      });
  }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2248967</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248967</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Thu, 06 Sep 2012 08:16:50 GMT</pubDate></item><item><title><![CDATA[Reply to Ping Pong Server mit boost::asio on Thu, 06 Sep 2012 08:45:06 GMT]]></title><description><![CDATA[<p>Danke, aber die n+1'ten vom Server kommenden Nachrichten sind immer noch leer. Die Ausgabe:</p>
<pre><code>Response: Thu Sep  6 10:41:21 2012
Response is empty.
Response is empty.
Response is empty.
Response is empty.
Response is empty.
Response is empty.
Response is empty.
Response is empty.
Response is empty.
Response is empty.
</code></pre>
<p>Irgendwo ist der Wurm drin. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
<pre><code class="language-cpp">void handle_response()
  {
    if (response_.size() &gt; 0)
      std::cout &lt;&lt; &quot;Response: &quot; &lt;&lt; &amp;response_;
    else
      std::cout &lt;&lt; &quot;Response is empty.&quot; &lt;&lt; std::endl;

    boost::asio::async_read(socket_, response_,
          boost::bind(&amp;client::handle_response, this));
  }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2248977</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248977</guid><dc:creator><![CDATA[goran]]></dc:creator><pubDate>Thu, 06 Sep 2012 08:45:06 GMT</pubDate></item><item><title><![CDATA[Reply to Ping Pong Server mit boost::asio on Thu, 06 Sep 2012 08:55:11 GMT]]></title><description><![CDATA[<p>Dann sendet der Server nur beim Connect des Clients ein Datum, danach aber nichts mehr. Laut Titel wolltest du ja aber eh einen Ping/Pong-Server schreiben, in dem Fall müsste dein Client auch jedesmal erst &quot;etwas&quot; (ein PING) senden, bevor der Server was antwortet.<br />
Außerdem sind zwei Async-Reads im handle_connect auch nicht das Gelbe vom Ei, damit schickst du quasi zwei &quot;parallele&quot; reads mitsamt den jeweiligen Reaktionen los, also quasi zwei Handlungsstränge.</p>
<p>Führ dir nochmal vor Augen, wie das mit den Completion-Handles in ASIO funktioniert...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2248986</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248986</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Thu, 06 Sep 2012 08:55:11 GMT</pubDate></item><item><title><![CDATA[Reply to Ping Pong Server mit boost::asio on Thu, 06 Sep 2012 09:22:18 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>Dann sendet der Server nur beim Connect des Clients ein Datum, danach aber nichts mehr. Laut Titel wolltest du ja aber eh einen Ping/Pong-Server schreiben, in dem Fall müsste dein Client auch jedesmal erst &quot;etwas&quot; (ein PING) senden, bevor der Server was antwortet.</p>
</blockquote>
<p>Ja, ich weiß, aber ich muss mich ran tasten. Aktuell wäre ein Ping-Pong-Server meinerseits so buggy das ich nicht wüsste wo anzufangen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2248997</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2248997</guid><dc:creator><![CDATA[goran]]></dc:creator><pubDate>Thu, 06 Sep 2012 09:22:18 GMT</pubDate></item></channel></rss>