Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SERVER STUFF
- if ((client = accept(server, (struct sockaddr *) 0, 0)) > 0) {
- printf("Connected to client\n");
- // Create a logged in variable
- int loggedIn = 0;
- int infoEntered = 0;
- // Store the username/password of the current user
- char username[50];
- char password[50];
- while (!loggedIn) {
- while (!infoEntered) {
- // Get username and password and makesure they're correct
- send(client, USERNAME_PROMPT, strlen(USERNAME_PROMPT)+1, 0);
- if (recv(client, username, 256, 0) > 0) {
- char *received = "Received Username\n";
- send(client, received, strlen(received), 0);
- // Received username, so ask for password
- send(client, PASSWORD_PROMPT, strlen(PASSWORD_PROMPT), 0);
- if (recv(client, password, 256, 0) > 0) {
- char *passwordr = "Received Password\n";
- send(client, passwordr, strlen(passwordr), 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);
- }
- }
- // 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;
- }
- // CLIENT STUFF
- int loggedIn = 0;
- // Connect to the server
- if (connect(server, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
- {
- perror("Could not connect to the server");
- close(server);
- exit(1);
- }
- while (gameRunning)
- {
- char myinfo[100];
- char message[256];
- // Loop until the user logs in or closes the program
- while (!loggedIn) {
- // Get the username
- // Can use read instead of recv?
- recv(server, message, sizeof(message), 0);
- printf(message);
- fgets(myinfo, strlen(myinfo), stdin);
- if (send(server, myinfo, strlen(myinfo), 0) > 0) {
- // Sent username to server so send password nowjo
- recv(server, message, sizeof(message), 0);
- // Get the password and send it
- printf(message);
- fgets(myinfo, strlen(myinfo), stdin);
- if (send(server, myinfo, strlen(myinfo), 0) > 0) {
- printf("Sent password");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement