【发布时间】:2015-03-28 16:06:54
【问题描述】:
我按照本教程 (http://codebase.eu/tutorial/linux-socket-programming-c/) 制作了一个服务器。问题是当服务器从客户端收到一个字符串时,我不知道如何比较它。例如,以下内容不起作用:
bytes_received = recv(new_sd, incomming_data_buffer, 1000, 0);
if(bytes_received == 0)
cout << "host shut down." << endl;
if(bytes_received == -1)
cout << "receive error!" << endl;
incomming_data_buffer[bytes_received] = '\0';
cout << "Received data: " << incomming_data_buffer << endl;
//The comparison in the if below doesn't work. The if isn't entered
//if the client sent "Hi", which should work
if(incomming_data_buffer == "Hi\n")
{
cout << "It said Hi!" << endl;
}
【问题讨论】:
-
你不能比较指针(数组衰减为指针,字符串文字是指向包含字符串的数组的指针),因为然后你比较 pointers 而不是它们指向。要么使用
std::string,要么使用std::strcmp。
标签: c++ networking client server