I am trying to send raw bits from a boost::dynamic_bitset with the Winsock2 sendto function. MS documentation shows that sendto uses const char * type for the buffer parameter. How do I send only the raw bits stored in the dynamic_bitset? I don't know how to cast or manipulate the dynamic_bitset so that it can be used in the sendto function.
Here are some relevant lines from my program:
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <system_error>
#include <boost/dynamic_bitset/dynamic_bitset.hpp>
#pragma comment(lib, "ws2_32.lib")
using boost::dynamic_bitset;
dynamic_bitset<> getDynamicBitset() {
//code to return a dynamic_bitset here
}
//create a dynamic bitset from a function
dynamic_bitset<> db1 = getDynamicBitset();
//manipulation of dynamic_bitset needed here to make it work in sendto as the buffer parameter
int ret = sendto(sock, buffer, len, flags, reinterpret_cast<SOCKADDR*>(&address), sizeof(address));
I am using the sample code from Peter R here: How do I receive udp packets with winsock in c++?
Also, my question is related to this, but I'm using C++ and not C. Sending a structure in the sendto() function - C language
I'm new to C++ and barely understand pointers and type manipulation.
Thanks.
I've implemented serialization for dynamic_bitset before: How to serialize boost::dynamic_bitset?
You can use that or a similar technique based on the same interfaces:
Which you could send like:
The analogous deserialization code:
Live Demo
Live On Coliru
Printing: