Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 通过套接字向客户端发送字符串_C++_Sockets - Fatal编程技术网

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

以下代码是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 += "      ";
    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++)