【发布时间】:2015-06-09 01:07:06
【问题描述】:
我正在为我的班级编写一个简单的 C++ 服务器程序。我们的教授给了我们要遵循的框架代码,以及 cmets 中的说明。我完成了它,它在 Unix 中编译,但我遇到了来自客户端的密码验证问题。它是这样写的,所以它会命中所有可能的选项,但仍然不打印结果。这是程序中的功能。 (如有必要,我可以提供整个功能)。所以我的问题,是我的逻辑有缺陷,还是一些小错误?
// PURPOSE: To send 'GOOD_PASSWORD_RESPONSE' to the client over socket file
// descriptor 'clientFD' and return 'true' if the password 'read()' from
// 'clientFD' matches 'password', or to send 'BAD_PASSWORD_RESPONSE' to
// the client and return 'false' otherwise.
bool didLogin(int clientFD, const char *password) {
// I. Application validity check:
printf("Process %d authenticating user . . .\n", getpid());
fflush(stdout);
// II. See if user successfully logged-in:
// II.A. Obtain user's password:
char buffer[MAX_LINE];
size_t size = sizeof(buffer);
read(clientFD, buffer, size);
for (int i = 0; i < sizeof(buffer); i++) {
printf("%c", buffer[i]);
}
// II.B. Handle when user's password does NOT match:
if (strncmp(buffer, password, MAX_PASSWORD_LEN) != 0) {
strncpy(BAD_PASSWORD_RESPONSE, buffer, MAX_LINE);
printf("Process %d bad password.\n", getpid());
return (false);
}
// II.C. If get here then user's password does match:
strncpy(GOOD_PASSWORD_RESPONSE, buffer, MAX_LINE);
printf("Process %d good password.\n", getpid());
// III. Finished:
return (true);
}
【问题讨论】:
-
关于如何不使用 C++ 的优秀示例;如何不进行身份验证;如果没有适当的初始化/大小参数,如何不使用原始缓冲区;如何不将业务逻辑与协议实现(传输细节)混合。提示:
read的返回值被忽略。那可能不应该发生 -
@sehe 大声笑,“可能不会发生”;它几乎搞砸了一切:)
-
感谢您的帮助,我知道这不是正确的方法,但我必须严格遵守提供的骨架代码。
-
能否在代码中留下贬义的cmets来弥补:/