use member function of socket class or common function in Boost::asio?












0














I am learning Boost::asio socket; I saw some examples, where they use the member function of socket class to read and receive messages, or use the boost::asio common function which passes the socket as the first param.



So I am wondering what the difference is between the two approaches? thanks!



//one kind
tcp::socket sock;
sock.async_write_some(***);

//another kind
boost::asio::async_write(socket,***);









share|improve this question





























    0














    I am learning Boost::asio socket; I saw some examples, where they use the member function of socket class to read and receive messages, or use the boost::asio common function which passes the socket as the first param.



    So I am wondering what the difference is between the two approaches? thanks!



    //one kind
    tcp::socket sock;
    sock.async_write_some(***);

    //another kind
    boost::asio::async_write(socket,***);









    share|improve this question



























      0












      0








      0







      I am learning Boost::asio socket; I saw some examples, where they use the member function of socket class to read and receive messages, or use the boost::asio common function which passes the socket as the first param.



      So I am wondering what the difference is between the two approaches? thanks!



      //one kind
      tcp::socket sock;
      sock.async_write_some(***);

      //another kind
      boost::asio::async_write(socket,***);









      share|improve this question















      I am learning Boost::asio socket; I saw some examples, where they use the member function of socket class to read and receive messages, or use the boost::asio common function which passes the socket as the first param.



      So I am wondering what the difference is between the two approaches? thanks!



      //one kind
      tcp::socket sock;
      sock.async_write_some(***);

      //another kind
      boost::asio::async_write(socket,***);






      c++ sockets boost boost-asio






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 2:16









      John Murray

      809514




      809514










      asked Nov 21 '18 at 1:52









      Gordon

      776




      776
























          1 Answer
          1






          active

          oldest

          votes


















          1














          async_write as static function guarantees that all data in buffer
          is written before this function returns.



          async_write_some as function member guarantees that at least one byte
          is written from buffer before this function ends.



          So if you want to use async_write_some you need to provide more code
          to handle the situation when not all data from buffer was written.



          Suppose you have string with 10 bytes, it is your buffer and you want to ensure
          all buffer is send:



          // Pseudocode:

          string str;
          // add 10 bytes to str
          SIZE = 10;
          int writtenBytes = 0;
          socket.async_write_some (buffer(str.c_str(),SIZE), makeCallback());

          void callback (
          error_code ec,
          size_t transferredBytes)
          {
          // check errors

          // [1]
          writtenBytes += transferredBytes;
          if (writtenBytes == SIZE)
          return;

          // call async_write_some
          socket.async_write_some (buffer(str.c_str()+writtenBytes,SIZE-writtenBytes),makeCallback());
          }


          in callback [1] you need to check how many bytes were written,
          if result is different from SIZE you need to call async_write_some again
          to send the remainder of data and so on, your callback may be invoked many times.



          The use of async_write is simpler:



          string str; // add 10 bytes
          async_write (buffer(str.c_str(),SIZE),makeCallback());

          void callback() {
          // check errors
          [1]
          }


          if no errors occured in [1] you know that all data was sent.






          share|improve this answer





















          • Thanks so much, according to your answer, async_write function seems a good choice, but what is the meaning of existence of async_write_some and async_read_some?
            – Gordon
            Nov 21 '18 at 8:12










          • Hmm, nice question. Boost Asio is wrapper for API which implements berkeley sockets, for example under linux you can use recv function which also doesn't guarantee that all buffer is received, it can return less bytes than was passed as argument to read. So async_read_some follows such behaviour.
            – rafix07
            Nov 21 '18 at 8:23











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404250%2fuse-member-function-of-socket-class-or-common-function-in-boostasio%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          async_write as static function guarantees that all data in buffer
          is written before this function returns.



          async_write_some as function member guarantees that at least one byte
          is written from buffer before this function ends.



          So if you want to use async_write_some you need to provide more code
          to handle the situation when not all data from buffer was written.



          Suppose you have string with 10 bytes, it is your buffer and you want to ensure
          all buffer is send:



          // Pseudocode:

          string str;
          // add 10 bytes to str
          SIZE = 10;
          int writtenBytes = 0;
          socket.async_write_some (buffer(str.c_str(),SIZE), makeCallback());

          void callback (
          error_code ec,
          size_t transferredBytes)
          {
          // check errors

          // [1]
          writtenBytes += transferredBytes;
          if (writtenBytes == SIZE)
          return;

          // call async_write_some
          socket.async_write_some (buffer(str.c_str()+writtenBytes,SIZE-writtenBytes),makeCallback());
          }


          in callback [1] you need to check how many bytes were written,
          if result is different from SIZE you need to call async_write_some again
          to send the remainder of data and so on, your callback may be invoked many times.



          The use of async_write is simpler:



          string str; // add 10 bytes
          async_write (buffer(str.c_str(),SIZE),makeCallback());

          void callback() {
          // check errors
          [1]
          }


          if no errors occured in [1] you know that all data was sent.






          share|improve this answer





















          • Thanks so much, according to your answer, async_write function seems a good choice, but what is the meaning of existence of async_write_some and async_read_some?
            – Gordon
            Nov 21 '18 at 8:12










          • Hmm, nice question. Boost Asio is wrapper for API which implements berkeley sockets, for example under linux you can use recv function which also doesn't guarantee that all buffer is received, it can return less bytes than was passed as argument to read. So async_read_some follows such behaviour.
            – rafix07
            Nov 21 '18 at 8:23
















          1














          async_write as static function guarantees that all data in buffer
          is written before this function returns.



          async_write_some as function member guarantees that at least one byte
          is written from buffer before this function ends.



          So if you want to use async_write_some you need to provide more code
          to handle the situation when not all data from buffer was written.



          Suppose you have string with 10 bytes, it is your buffer and you want to ensure
          all buffer is send:



          // Pseudocode:

          string str;
          // add 10 bytes to str
          SIZE = 10;
          int writtenBytes = 0;
          socket.async_write_some (buffer(str.c_str(),SIZE), makeCallback());

          void callback (
          error_code ec,
          size_t transferredBytes)
          {
          // check errors

          // [1]
          writtenBytes += transferredBytes;
          if (writtenBytes == SIZE)
          return;

          // call async_write_some
          socket.async_write_some (buffer(str.c_str()+writtenBytes,SIZE-writtenBytes),makeCallback());
          }


          in callback [1] you need to check how many bytes were written,
          if result is different from SIZE you need to call async_write_some again
          to send the remainder of data and so on, your callback may be invoked many times.



          The use of async_write is simpler:



          string str; // add 10 bytes
          async_write (buffer(str.c_str(),SIZE),makeCallback());

          void callback() {
          // check errors
          [1]
          }


          if no errors occured in [1] you know that all data was sent.






          share|improve this answer





















          • Thanks so much, according to your answer, async_write function seems a good choice, but what is the meaning of existence of async_write_some and async_read_some?
            – Gordon
            Nov 21 '18 at 8:12










          • Hmm, nice question. Boost Asio is wrapper for API which implements berkeley sockets, for example under linux you can use recv function which also doesn't guarantee that all buffer is received, it can return less bytes than was passed as argument to read. So async_read_some follows such behaviour.
            – rafix07
            Nov 21 '18 at 8:23














          1












          1








          1






          async_write as static function guarantees that all data in buffer
          is written before this function returns.



          async_write_some as function member guarantees that at least one byte
          is written from buffer before this function ends.



          So if you want to use async_write_some you need to provide more code
          to handle the situation when not all data from buffer was written.



          Suppose you have string with 10 bytes, it is your buffer and you want to ensure
          all buffer is send:



          // Pseudocode:

          string str;
          // add 10 bytes to str
          SIZE = 10;
          int writtenBytes = 0;
          socket.async_write_some (buffer(str.c_str(),SIZE), makeCallback());

          void callback (
          error_code ec,
          size_t transferredBytes)
          {
          // check errors

          // [1]
          writtenBytes += transferredBytes;
          if (writtenBytes == SIZE)
          return;

          // call async_write_some
          socket.async_write_some (buffer(str.c_str()+writtenBytes,SIZE-writtenBytes),makeCallback());
          }


          in callback [1] you need to check how many bytes were written,
          if result is different from SIZE you need to call async_write_some again
          to send the remainder of data and so on, your callback may be invoked many times.



          The use of async_write is simpler:



          string str; // add 10 bytes
          async_write (buffer(str.c_str(),SIZE),makeCallback());

          void callback() {
          // check errors
          [1]
          }


          if no errors occured in [1] you know that all data was sent.






          share|improve this answer












          async_write as static function guarantees that all data in buffer
          is written before this function returns.



          async_write_some as function member guarantees that at least one byte
          is written from buffer before this function ends.



          So if you want to use async_write_some you need to provide more code
          to handle the situation when not all data from buffer was written.



          Suppose you have string with 10 bytes, it is your buffer and you want to ensure
          all buffer is send:



          // Pseudocode:

          string str;
          // add 10 bytes to str
          SIZE = 10;
          int writtenBytes = 0;
          socket.async_write_some (buffer(str.c_str(),SIZE), makeCallback());

          void callback (
          error_code ec,
          size_t transferredBytes)
          {
          // check errors

          // [1]
          writtenBytes += transferredBytes;
          if (writtenBytes == SIZE)
          return;

          // call async_write_some
          socket.async_write_some (buffer(str.c_str()+writtenBytes,SIZE-writtenBytes),makeCallback());
          }


          in callback [1] you need to check how many bytes were written,
          if result is different from SIZE you need to call async_write_some again
          to send the remainder of data and so on, your callback may be invoked many times.



          The use of async_write is simpler:



          string str; // add 10 bytes
          async_write (buffer(str.c_str(),SIZE),makeCallback());

          void callback() {
          // check errors
          [1]
          }


          if no errors occured in [1] you know that all data was sent.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 6:19









          rafix07

          6,6531614




          6,6531614












          • Thanks so much, according to your answer, async_write function seems a good choice, but what is the meaning of existence of async_write_some and async_read_some?
            – Gordon
            Nov 21 '18 at 8:12










          • Hmm, nice question. Boost Asio is wrapper for API which implements berkeley sockets, for example under linux you can use recv function which also doesn't guarantee that all buffer is received, it can return less bytes than was passed as argument to read. So async_read_some follows such behaviour.
            – rafix07
            Nov 21 '18 at 8:23


















          • Thanks so much, according to your answer, async_write function seems a good choice, but what is the meaning of existence of async_write_some and async_read_some?
            – Gordon
            Nov 21 '18 at 8:12










          • Hmm, nice question. Boost Asio is wrapper for API which implements berkeley sockets, for example under linux you can use recv function which also doesn't guarantee that all buffer is received, it can return less bytes than was passed as argument to read. So async_read_some follows such behaviour.
            – rafix07
            Nov 21 '18 at 8:23
















          Thanks so much, according to your answer, async_write function seems a good choice, but what is the meaning of existence of async_write_some and async_read_some?
          – Gordon
          Nov 21 '18 at 8:12




          Thanks so much, according to your answer, async_write function seems a good choice, but what is the meaning of existence of async_write_some and async_read_some?
          – Gordon
          Nov 21 '18 at 8:12












          Hmm, nice question. Boost Asio is wrapper for API which implements berkeley sockets, for example under linux you can use recv function which also doesn't guarantee that all buffer is received, it can return less bytes than was passed as argument to read. So async_read_some follows such behaviour.
          – rafix07
          Nov 21 '18 at 8:23




          Hmm, nice question. Boost Asio is wrapper for API which implements berkeley sockets, for example under linux you can use recv function which also doesn't guarantee that all buffer is received, it can return less bytes than was passed as argument to read. So async_read_some follows such behaviour.
          – rafix07
          Nov 21 '18 at 8:23


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404250%2fuse-member-function-of-socket-class-or-common-function-in-boostasio%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ottavio Pratesi

          Tricia Helfer

          15 giugno