无法使用C中的BSD套接字进行身份验证

时间:2015-10-13 09:59:50

标签: c sockets authentication bsd

这里很新。希望你们可以帮助我:<

我在验证用户时遇到问题。最终目标是拥有以下内容:

1)客户端连接到服务器

2)服务器向客户端发送用户名请求,客户端用用户名

回复

3)服务器向客户端发送密码请求,客户端回复密码

4)服务器对某些内容进行检查(目前仅进行一次测试比较,所以"如果用户名==测试,密码==测试"

5)这会向客户端发送一个值(如1,登录,0表示已登录),如果登录为1,则客户端退出while循环并执行其余代码,否则如果为0则客户端再次循环,直到用户退出程序,或输入正确的用户名/密码。

最后,程序将从一个看起来像

的文本文件中读入用户名/密码

user,pass / user1,pass1 - 在这种情况下,服务器只会与所有行和密码进行比较,但这是后来的另一个问题。

现在我的客户端/服务器要求输入用户名和密码,您可以输入它们,但我无法获得第4步和第4步。 5工作。 (1为登录,0为不正确的用户名/密码)我觉得我错过了插座的东西。比如每次发送后清除一个套接字?

我的代码如下:

server.c:

while (!loggedIn) {
                while (!infoEntered) {
                    // Get username and password and makesure they're correct
                    send(client, "Please enter a username:\n", 256, 0);
                    if (recv(client, username, 256, 0) > 0) {
                        // Received username, so ask for password
                        send(client, "Please enter a password:\n", 256, 0);
                        if (recv(client, password, 256, 0) > 0) {
                            // Username and password have been entered ready for checking
                            infoEntered = 1;
                            break;
                        } else {
                            printf("Could not receive password for %s", username);
                            close(client);
                        }
                    } else {
                        printf("Could not receive the username, please reconnect and try again.");
                        close(client);
                    }
                }

                // send a test message
                send(client, "hello", 256, 0);

                char test[256];
                recv(client, test, 256, 0);
                printf("Got: %s", test);

                // I need to check login here
                // Check map/hashmap of username and passwords
                // If the username is in the file, confirm password
                // If password is correct, send a signal to the client
                // Then set logged in to 1, to start the hangman game
                loggedIn = 1;
            }

完整的server.c代码:http://pastebin.com/37YiZgWu

client.c:

// Loop until the user logs in or closes the program
        while (!loggedIn) {
            // Get the username
            receive = recv(server, serverR, sizeof(serverR), 0);
            printf("Server: %s", serverR);
            fgets(client, 255, stdin);
            if (send(server, client, sizeof(client), 0) > 0) {
                // Sent username to server so send password now
                receive = recv(server, serverR, sizeof(serverR), 0);
                // Get the password and send it
                printf("Server: %s", serverR);
                fgets(client, 255, stdin);
                if (send(server, client, sizeof(client), 0) > 0) {
                    char loggedReply[256];
                    recv(server, loggedReply, 256, 0);
                    if (loggedReply == "hello") {
                        send(server, "got into logged reply", 256, 0);
                        loggedIn = 1;
                    } else {
                        loggedIn = 0;
                    }
                }
            }
        }

完整的客户代码:http://pastebin.com/p5ZFGhDJ

先谢谢了:(我通常不会寻求帮助,但我花了2个小时试图让它正常工作......

编辑回复了以下关于澄清"最后一部分":

的评论

"最后一部分是实际的身份验证部分。所以在服务器收到用户名/密码之后的所有内容。在此之后我做的任何其他发送/接收只是简单地不工作而我不知道为什么。我需要服务器运行if语句来比较用户名/密码,然后在两种情况下,向客户端发送一个信号,所以一个单词,整数,布尔值,等等,在这种情况下客户端将等待这个信号(看看它是否已登录),然后如果登录,则在循环中退出登录,运行程序,或者重新循环询问用户名/再次传递"

2 个答案:

答案 0 :(得分:0)

有一个用于身份验证的库:GSASL

here

否则我不明白这个错误。也许您应该使用也在GSASL中实现的常见会话机制

precedence

答案 1 :(得分:0)

c函数fgets()将换行“\ n”字符作为有效输入。因此,您向服务器发送“用户名”+“\ n”“密码”+“\ n”。 在将sendint发送到服务器之前清理字符串。

相关问题