Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can send structs over socket, but you have to serialize them before sending the struct using boost serialization.</p> <p>Here is a sample code :</p> <pre><code> #include&lt;iostream&gt; #include&lt;unistd.h&gt; #include&lt;cstring&gt; #include &lt;sstream&gt; #include &lt;boost/archive/text_oarchive.hpp&gt; #include &lt;boost/archive/text_iarchive.hpp&gt; using namespace std; typedef struct { public: int id; int message_length; string message_str; private: friend class boost::serialization::access; template &lt;typename Archive&gt; void serialize(Archive &amp;ar, const unsigned int vern) { ar &amp; id; ar &amp; message_length; ar &amp; message_str; } } Message; int main() { Message newMsg; newMsg.id = 7; newMsg.message_length = 14; newMsg.message_str="Hi ya Whats up"; std::stringstream strData; boost::archive::text_oarchive oa(strData); oa &lt;&lt; newMsg; char *serObj = (char*) strData.str().c_str(); cout &lt;&lt; "Serialized Data ::: " &lt;&lt; serObj &lt;&lt; "Len ::: " &lt;&lt; strlen(serObj) &lt;&lt; "\n"; /* Send serObj thru Sockets */ /* recv serObj from socket &amp; deserialize it */ std::stringstream rcvdObj(serObj); Message deserObj; boost::archive::text_iarchive ia(rcvdObj); ia &gt;&gt; deserObj; cout&lt;&lt;"id ::: "&lt;&lt;deserObj.id&lt;&lt;"\n"; cout&lt;&lt;"len ::: "&lt;&lt;deserObj.message_length&lt;&lt;"\n"; cout&lt;&lt;"str ::: "&lt;&lt;deserObj.message_str&lt;&lt;"\n"; } </code></pre> <p>you can compile the program by</p> <p>g++ -o serial boost.cpp /usr/local/lib/libboost_serialization.a </p> <p>you must have libboost_serialization.a statically compiled in your machine.</p> <p>Keeping the sockets 'blocking' will be good and you have to devise for reading these structs from recv buffer.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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