int dataLength;
int readLength = read(conn->getFd(), &dataLength, HEADER_SIZE);
printf("Header read %d \n", readLength);
if(readLength == 0){
removeClient(conn);
close(conn->getFd());
}
else {
/*
* Do not parse anything here.
* Pass the raw data to the work handler.
*/
char *buf = new char[dataLength+1];
int dataReadLength = read(conn->getFd(), buf, dataLength);
printf("Data read %d \n", dataReadLength);
printf("ServerManager::eventAcceptLoop, A message has been received \n");
addWork(conn->getFd(), buf, dataReadLength);
我正在从事网络编程工作,我有这段代码。它在 localhost 中运行良好(没有错误发生),但我确信如果我在真实世界的网络上尝试它会崩溃。问题是我不是网络编程专家,也不确定应该添加哪个错误句柄。它还必须找到一个很好的示例或教程,包括特定的错误处理。
如果你们能为我提供一个可能有助于获得错误处理的好示例或教程,我将不胜感激。
提前致谢。
最佳答案
需要分析read()
返回-1
的情况。
查看本教程/引用资料:Beej's Guide to Network Programming 这是我进行网络编程的首选引用资料。
关于c++ - 读句柄错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6420461/