Reputation: 3208
I have successfully started using a socket in C++ , and connected to it in a python client. I started by sending just plain strings and stopping the C++ Boost whenever it encountered the delimiter (In my case I used
// C++ boost Socket Server
std::string read_socket_command(tcp::socket & socket) {
boost::asio::streambuf buf;
boost::asio::read_until( socket, buf, "\n" );
std::string data = boost::asio::buffer_cast<std::string*>(buf.data());
return data;
}
http://think-async.com/Asio/boost_asio_1_12_1/doc/html/boost_asio/overview/core/line_based.html
my problem is, now in the Python Client I want to send over data as a struct, packed. But how do I tell the C++ code to stop reading data?
# Python Client
import struct
DATA_NAME_String = "string int int int long long int int int"
DATA_FORMAT_STRING = "25s I I I I I I I I"
def pack_data(msg):
debug_print("Packing a message...")
if type(msg) is SomeCustomType:
data = ("hi", 22, 1, 4, 457, 42342, 0, 1)
packer = struct.Struct(DATA_FORMAT_STRING)
packed_data = packer.pack(*data)
return packed_data
Upvotes: 1
Views: 439
Reputation: 1675
In your packed data let the first 4 bytes(uint32_t) indicate the number of bytes of the payload. In a nutshell in sending binary data you should send the other party the size upfront. read_until
is valid for a text protocol, but for binary data you probably have to use read
.
Upvotes: 3