dzungchaos

TCP_Client.cpp

Nov 17th, 2020
1,670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.72 KB | None | 0 0
  1. // LoginClient.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <conio.h>
  7. #include <winsock2.h>
  8. #include <ws2tcpip.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #define _CRT_SECURE_NO_WARNINGS
  12. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  13. #define SERVER_PORT 7225
  14. #define SERVER_ADDR "127.0.0.1"
  15. #define BUFF_SIZE 2048
  16. #undef UNICODE
  17.  
  18. //Error code for USERID
  19. #define USER_OK "00"   
  20. #define USER_VOID "01"      // User doesnot exist
  21. #define USER_DISABLED "02"  // User is locked
  22. #define USER_NULL "03"      // User has not entered UID
  23.  
  24. // Error code for PASSWORD
  25. #define PASS_OK "10"
  26. #define PASS_INCORRECT "11"
  27. #define PASS_NULL "12"
  28.  
  29. // Error code for LOGOUT
  30. #define LOGOUT_SUCCESS "20"
  31. #define USER_NOT_AUTHENTICATED "21"
  32.  
  33. // Error code for LOGIN
  34. #define LOGIN_SUCCESS "30"
  35. #define USER_AUTHENTICATED "31"
  36.  
  37. // Error code for EXIT
  38. #define EXIT_OK  "40"
  39. #define EXIT_FAIL "41"
  40.  
  41. // Other error code
  42. #define SYNTAX_ERROR "51"
  43. #define NO_OPTION "52"
  44.  
  45. // Code for kind of message
  46. #define USER_PREFIX "USER"
  47. #define PASSWORD_PREFIX "PASS"
  48. #define LOGOUT_PREFIX "LOUT"
  49. #define LOGIN_PREFIX "LGIN"
  50. #define EXIT_PREFIX "EXIT"
  51. #define OTHER_OPTION "OTHR"
  52.  
  53. // Status for connection
  54. #define AUTHENTICATED 0
  55. #define NOT_AUTHENTICATED 1
  56. #define NOT_IDENTIFIED 2
  57. #define NO_CONNECTION 3
  58.  
  59. // link with Ws2_32.lib
  60. #pragma comment (lib, "Ws2_32.lib")
  61. #pragma warning(disable : 4996)
  62.  
  63. void printMenu();
  64. void chopNChars(char *, int);
  65. void logIn(SOCKET);
  66. void logOut(SOCKET);
  67. void exitProgram(SOCKET);
  68.  
  69. int main(int argc, char *argv[]) {
  70.     // Initiate WinSock
  71.     WSADATA wsaData;
  72.     WORD wVersion = MAKEWORD(2, 2);
  73.     if (WSAStartup(wVersion, &wsaData)) {
  74.         printf("Version is not supported!\n");
  75.     }
  76.  
  77.     // Specify server address
  78.     sockaddr_in serverAddr;
  79.     serverAddr.sin_family = AF_INET;
  80.     serverAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
  81.     serverAddr.sin_port = htons(SERVER_PORT);
  82.  
  83.     // Construct socket
  84.     SOCKET client;
  85.     client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  86.     // (optional) Set time-out for receiving
  87.     int tv = 10000; // Time-out interval: 10000ms
  88.     setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, (const char *)(&tv), sizeof(int));
  89.  
  90.     // Message to send to client
  91.     char choice[BUFF_SIZE];
  92.  
  93.     // Request to connect server
  94.     if (connect(client, (sockaddr *)&serverAddr, sizeof(serverAddr))) {
  95.         printf("Error! Cannot connect to server!\nError code: %d\n", WSAGetLastError());
  96.         return 0;
  97.     }
  98.     printf("Server connected!\n");
  99.  
  100.     while (1) {
  101.  
  102.         // Print MENU
  103.         printMenu();
  104.  
  105.         // Choose function of the system
  106.         printf("Your option: ");
  107.         gets_s(choice, BUFF_SIZE);
  108.  
  109.         // Send log in request to server
  110.         if (atoi(choice) == 1) {
  111.             logIn(client);
  112.         }
  113.  
  114.         // Send log out request to server
  115.         else if (atoi(choice) == 2) {
  116.             logOut(client);
  117.         }
  118.  
  119.         // Send exit request to server
  120.         else if (atoi(choice) == 3) {
  121.             exitProgram(client);
  122.             return 0;
  123.         }
  124.  
  125.         // Notice the error
  126.         else
  127.             printf("No such option! Please try again!\n");
  128.  
  129.     }
  130.  
  131.     // Close socket
  132.     closesocket(client);
  133.  
  134.     // Terminate Winsock
  135.     WSACleanup();
  136.  
  137.     return 0;
  138. }
  139.  
  140. // Function to cut n first character of
  141. // a pointed string
  142. void chopNChars(char *str, int n) {
  143.     int strLen = strlen(str);
  144.     if (n > strLen)
  145.         return;
  146.     memmove(str, str + n, strLen - n + 1);
  147. }
  148.  
  149. // Funtion to print menu to client console
  150. void printMenu() {
  151.     printf("--------MENU--------\n");
  152.     printf("1. Login system\n");
  153.     printf("2. Logout\n");
  154.     printf("3. Exit\n");
  155.     printf("---------------------\n");
  156. }
  157.  
  158. // Funtion to send username and password (login) to server
  159. void logIn(SOCKET client) {
  160.     int ret;
  161.  
  162.     char userID[] = USER_PREFIX;
  163.     char buff[BUFF_SIZE] = { '\0' };
  164.     // Enter UserID
  165.     printf("USERNAME:");
  166.     gets_s(buff, BUFF_SIZE);
  167.     strcat(userID, buff);
  168.     // Send UserID to server
  169.     ret = send(client, userID, strlen(userID), 0);
  170.     if (ret == SOCKET_ERROR)
  171.         printf("Error! Cannot send message!\nError code: %i\n", WSAGetLastError());
  172.  
  173.     chopNChars(userID, 4);
  174.  
  175.     // Receive echo message
  176.     ret = recv(client, buff, BUFF_SIZE, 0);
  177.     buff[ret] = 0;
  178.     if (ret == SOCKET_ERROR) {
  179.         if (WSAGetLastError() == WSAETIMEDOUT)
  180.             printf("Time-out!\n");
  181.     }
  182.     else if (strlen(buff) > 0) {
  183.         // Check received code to know if the username is valid
  184.         if (strcmp(buff, USER_OK) == 0) {
  185.             // UserID has not been authenticated
  186.             int countLeft = 3;
  187.  
  188.             // Enter password
  189.             printf("PASSWORD:");
  190.             char passwd[] = PASSWORD_PREFIX;
  191.             gets_s(buff, BUFF_SIZE);
  192.             strcat(passwd, buff);
  193.  
  194.             // Send password to server
  195.             ret = send(client, passwd, strlen(passwd), 0);
  196.  
  197.             char buff2[BUFF_SIZE] = "\0";
  198.             // Receive echo message
  199.             ret = recv(client, buff2, BUFF_SIZE, 0);
  200.             if (WSAGetLastError() == WSAETIMEDOUT)
  201.                 printf("Time-out!\n");
  202.  
  203.             buff2[ret] = 0;
  204.             // Check if the password is correct
  205.             if (strcmp(buff2, PASS_OK) == 0) {
  206.                 printf("Loggin in to the system!\n\n");
  207.             }
  208.  
  209.             // The password is incorrect, warn user
  210.             else if ((strcmp(buff2, PASS_INCORRECT) == 0) && (countLeft > 0)) {
  211.                 countLeft--;
  212.                 printf("Wrong password! %d times left!\n", countLeft);
  213.             }
  214.  
  215.             // The userID has been locked from the system
  216.             else if (strcmp(buff2, USER_DISABLED) == 0) {
  217.                 printf("UserID %s is locked! Please contact with admin for support!\n", userID);
  218.             }
  219.         }
  220.  
  221.         // The userID does not exist
  222.         else if (strcmp(buff, USER_VOID) == 0) {
  223.             printf("UserID not found!\n");
  224.         }
  225.  
  226.         // The userID has been locked from the system
  227.         else if (strcmp(buff, USER_DISABLED) == 0) {
  228.             printf("UserID %s is locked! Please contact with admin for support!\n", userID);
  229.         }
  230.     }
  231. }
  232.  
  233. // Funtion to send logout request to server
  234. void logOut(SOCKET client) {
  235.     int ret;
  236.     char option2[BUFF_SIZE];
  237.     ret = send(client, LOGOUT_PREFIX, strlen(LOGOUT_PREFIX), 0);
  238.     if (ret == SOCKET_ERROR)
  239.         printf("Error! Cannot send message!\nError code: %i\n", WSAGetLastError());
  240.  
  241.     ret = recv(client, option2, BUFF_SIZE, 0);
  242.     if (WSAGetLastError() == WSAETIMEDOUT)
  243.         printf("Time-out!\n");
  244.  
  245.     option2[ret] = 0;
  246.  
  247.     // Check if the logout request is success
  248.     if (strcmp(option2, LOGOUT_SUCCESS) == 0) {
  249.         printf("Logging out ... \n\n");
  250.     }
  251.     else if (strcmp(option2, USER_NOT_AUTHENTICATED) == 0) {
  252.         printf("No account has logged in!\n\n");
  253.     }
  254. }
  255.  
  256. // Function to send exit request to server
  257. void exitProgram(SOCKET client) {
  258.     int ret;
  259.     char option3[BUFF_SIZE];
  260.     ret = send(client, EXIT_PREFIX, strlen(EXIT_PREFIX), 0);
  261.     if (ret == SOCKET_ERROR)
  262.         printf("Error! Cannot send message!\nError code: %i\n", WSAGetLastError());
  263.  
  264.     ret = recv(client, option3, BUFF_SIZE, 0);
  265.     if (WSAGetLastError() == WSAETIMEDOUT)
  266.         printf("Time-out!\n");
  267.  
  268.     option3[ret] = 0;
  269.  
  270.     if (strcmp(option3, EXIT_OK) == 0)
  271.         printf("Exitting ... \n\n");
  272. }
Advertisement
Add Comment
Please, Sign In to add comment