[英]boost sockets read_until for structs
我已經成功開始使用C ++中的套接字,並在python客戶端中連接到它。 我首先發送純字符串,然后在遇到定界符時停止C ++ Boost(就我而言,
// 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
我的問題是,現在在Python客戶端中,我想打包打包發送數據作為結構。 但是,如何告訴C ++代碼停止讀取數據?
# 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
在打包數據中,讓前4個字節(uint32_t)指示有效負載的字節數。 簡而言之,在發送二進制數據時,您應該提前發送另一方的大小。 read_until
對於文本協議有效,但對於二進制數據,您可能必須使用read
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.