我正在为我的课程使用C++的简单服务器程序。我们的教授给我们提供了框架代码,以及评论中的指示。我完成了它,并且它在Unix中编译,但是我遇到了来自客户端的密码认证问题。它是这样写的,它可以打印所有可能的选项,但仍不会打印结果。这是程序的功能。 (如果需要,我可以提供整个功能)。所以我的问题是,我的逻辑是否有缺陷,或者是一些小错误?TCP服务器C++程序
// 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
@sehe大声笑,'应该可能不会发生';它几乎拧了一切:) –
感谢您的帮助,我知道它不是正确的做法,但我必须严格遵守提供的骨架代码。 –