C++ 通过套接字向客户端发送字符串
以下代码是server.c中的一段代码C++ 通过套接字向客户端发送字符串,c++,sockets,C++,Sockets,以下代码是server.c中的一段代码 void list(int s) { vector<data> getList = readContent(); string output = ""; ostringstream conv; for(int j = 0; j < readContent().size(); j++) { conv << getList[j].record; output += conv.str(); output
void list(int s)
{
vector<data> getList = readContent();
string output = "";
ostringstream conv;
for(int j = 0; j < readContent().size(); j++)
{
conv << getList[j].record;
output += conv.str();
output += " ";
output += getList[j].fName;
output += " ";
output += getList[j].lName;
output += " ";
output += getList[j].phoneNum;
output += "\n";
send(s, (char *)output.c_str(), strlen((char *)output.c_str()), 0);
}
}
套接字设置正确,但它没有将数据发送到客户端进行打印,有人知道出了什么问题吗?
readContent
从何处读取数据?因为这条线看起来很可疑
for(int j = 0; j < readContent().size(); j++)
for(int j=0;j
当然应该是这样
for(int j = 0; j < getList.size(); j++)
for(int j=0;j
因为否则您将多次调用
readContent
。您应该检查send
和recv
的返回代码。此外,这些函数不能确保发送或接收整个缓冲区,因此必须使用循环发送和接收数据。此外,您总是将数据附加到输出
,并且从不在循环中重置此变量的值,即在发送之后。
for(int j = 0; j < getList.size(); j++)