[英]Cannot send ostringstream variable over socket in C++
有人可以向我解釋為什么我無法通過套接字發送使用ostringstream
派生的string
變量嗎?
std::ostringstream oss1;
std::ostringstream oss2;
int code = 52;
oss1 << "4" << "1" << "0" << "0" << "0" << "0" << 224 + code / 16 << code % 16;
oss2 << "4" << "0" << "0" << "0" << "0" << "0" << 224 + code / 16 << code % 16;
int msg_len3 = oss1.tellp;
int msg_len4 = oss2.tellp;
std::string var1 = oss1.str();
std::string var2 = oss2.str();
comm_send1 = send(sock, var1, msg_len3, 0);
comm_send2 = send(sock, var2, msg_len4, 0);
有了這段代碼,我得到一個錯誤:
不存在從std :: string到const char *的合適轉換函數
因為send()
函數需要const char *
參數,而不是std::string
,這是.str()
給您的。
嘗試以下方法:
comm_send1 = send(sock, var1.c_str(), msg_len3, 0);
std::string
的.c_str()
成員函數為您提供所需的類型:C樣式字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.