我在 c++(服务器)和 php(客户端)中的套接字有一些问题。下面是代码。问题是,每当客户端断开连接(由于失败或其他原因)时,pClient->send() 函数应该返回 -1 或 0。但是,它没有。因此我无法打破循环,并且程序在此功能上失败。我究竟做错了什么?我希望你能帮帮我!
int sentSucces=0;
char szData[MAX_PACKET];
memset(szData, 0, MAX_PACKET);
CPassiveSocket socket;
CActiveSocket *pClient = NULL;
sendInterval = 10;
socket.Initialize();
socket.Listen((const uint8 *) "127.0.0.1", 9876);
socket.SetNonblocking(); //if no connection in queue, don't wait (accept())
while (1) {
//check queue for connections.
if ((pClient = socket.Accept()) != NULL) {
pClient->SetNonblocking();
cout << "++ connected\n";
while (1) {
// Receive request from the client.
cout << "- receiving....\n";
if (pClient->Receive(MAX_PACKET) > 0) {
cout << "-- received\n";
strncpy(szData, (const char *) pClient->GetData(), pClient->GetBytesReceived());
cout << "-- +: " << szData << "\n";
//clear memory of szData
memset(szData, 0, MAX_PACKET);
} else {
cout << "-- NOT received\n";
}
string bericht = "hallo";
cout << "sending....\n";
sentSucces = pClient->Send((const uint8 *) bericht.c_str(), strlen(bericht.c_str()));
cout << "++ send:" << sentSucces << "\n";
if (sentSucces < 1) break;
sleep(4);
}
cout << "++ disconnecting...\n";
pClient->Close();
cout << "++ disconnected\n";
} else {
// cout << "++ no connections\n";
}
usleep(10000);
}