Advertisement
Guest User

Server/client

a guest
Oct 13th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. // SERVER STUFF
  2.  
  3. if ((client = accept(server, (struct sockaddr *) 0, 0)) > 0) {
  4. printf("Connected to client\n");
  5.  
  6. // Create a logged in variable
  7. int loggedIn = 0;
  8. int infoEntered = 0;
  9.  
  10. // Store the username/password of the current user
  11. char username[50];
  12. char password[50];
  13.  
  14. while (!loggedIn) {
  15. while (!infoEntered) {
  16. // Get username and password and makesure they're correct
  17. send(client, USERNAME_PROMPT, strlen(USERNAME_PROMPT)+1, 0);
  18. if (recv(client, username, 256, 0) > 0) {
  19. char *received = "Received Username\n";
  20. send(client, received, strlen(received), 0);
  21. // Received username, so ask for password
  22. send(client, PASSWORD_PROMPT, strlen(PASSWORD_PROMPT), 0);
  23. if (recv(client, password, 256, 0) > 0) {
  24. char *passwordr = "Received Password\n";
  25. send(client, passwordr, strlen(passwordr), 0);
  26. // Username and password have been entered ready for checking
  27. infoEntered = 1;
  28. break;
  29. } else {
  30. printf("Could not receive password for %s", username);
  31. close(client);
  32. }
  33. } else {
  34. printf("Could not receive the username, please reconnect and try again.");
  35. close(client);
  36. }
  37. }
  38.  
  39. // I need to check login here
  40. // Check map/hashmap of username and passwords
  41. // If the username is in the file, confirm password
  42. // If password is correct, send a signal to the client
  43. // Then set logged in to 1, to start the hangman game
  44. loggedIn = 1;
  45. }
  46.  
  47. // CLIENT STUFF
  48.  
  49. int loggedIn = 0;
  50.  
  51. // Connect to the server
  52. if (connect(server, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
  53. {
  54. perror("Could not connect to the server");
  55. close(server);
  56. exit(1);
  57. }
  58.  
  59. while (gameRunning)
  60. {
  61. char myinfo[100];
  62. char message[256];
  63.  
  64. // Loop until the user logs in or closes the program
  65. while (!loggedIn) {
  66. // Get the username
  67. // Can use read instead of recv?
  68. recv(server, message, sizeof(message), 0);
  69. printf(message);
  70. fgets(myinfo, strlen(myinfo), stdin);
  71. if (send(server, myinfo, strlen(myinfo), 0) > 0) {
  72. // Sent username to server so send password nowjo
  73. recv(server, message, sizeof(message), 0);
  74. // Get the password and send it
  75. printf(message);
  76. fgets(myinfo, strlen(myinfo), stdin);
  77. if (send(server, myinfo, strlen(myinfo), 0) > 0) {
  78. printf("Sent password");
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement