Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LoginClient.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <conio.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <string.h>
- #include <stdlib.h>
- #define _CRT_SECURE_NO_WARNINGS
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #define SERVER_PORT 7225
- #define SERVER_ADDR "127.0.0.1"
- #define BUFF_SIZE 2048
- #undef UNICODE
- //Error code for USERID
- #define USER_OK "00"
- #define USER_VOID "01" // User doesnot exist
- #define USER_DISABLED "02" // User is locked
- #define USER_NULL "03" // User has not entered UID
- // Error code for PASSWORD
- #define PASS_OK "10"
- #define PASS_INCORRECT "11"
- #define PASS_NULL "12"
- // Error code for LOGOUT
- #define LOGOUT_SUCCESS "20"
- #define USER_NOT_AUTHENTICATED "21"
- // Error code for LOGIN
- #define LOGIN_SUCCESS "30"
- #define USER_AUTHENTICATED "31"
- // Error code for EXIT
- #define EXIT_OK "40"
- #define EXIT_FAIL "41"
- // Other error code
- #define SYNTAX_ERROR "51"
- #define NO_OPTION "52"
- // Code for kind of message
- #define USER_PREFIX "USER"
- #define PASSWORD_PREFIX "PASS"
- #define LOGOUT_PREFIX "LOUT"
- #define LOGIN_PREFIX "LGIN"
- #define EXIT_PREFIX "EXIT"
- #define OTHER_OPTION "OTHR"
- // Status for connection
- #define AUTHENTICATED 0
- #define NOT_AUTHENTICATED 1
- #define NOT_IDENTIFIED 2
- #define NO_CONNECTION 3
- // link with Ws2_32.lib
- #pragma comment (lib, "Ws2_32.lib")
- #pragma warning(disable : 4996)
- void printMenu();
- void chopNChars(char *, int);
- void logIn(SOCKET);
- void logOut(SOCKET);
- void exitProgram(SOCKET);
- int main(int argc, char *argv[]) {
- // Initiate WinSock
- WSADATA wsaData;
- WORD wVersion = MAKEWORD(2, 2);
- if (WSAStartup(wVersion, &wsaData)) {
- printf("Version is not supported!\n");
- }
- // Specify server address
- sockaddr_in serverAddr;
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
- serverAddr.sin_port = htons(SERVER_PORT);
- // Construct socket
- SOCKET client;
- client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- // (optional) Set time-out for receiving
- int tv = 10000; // Time-out interval: 10000ms
- setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, (const char *)(&tv), sizeof(int));
- // Message to send to client
- char choice[BUFF_SIZE];
- // Request to connect server
- if (connect(client, (sockaddr *)&serverAddr, sizeof(serverAddr))) {
- printf("Error! Cannot connect to server!\nError code: %d\n", WSAGetLastError());
- return 0;
- }
- printf("Server connected!\n");
- while (1) {
- // Print MENU
- printMenu();
- // Choose function of the system
- printf("Your option: ");
- gets_s(choice, BUFF_SIZE);
- // Send log in request to server
- if (atoi(choice) == 1) {
- logIn(client);
- }
- // Send log out request to server
- else if (atoi(choice) == 2) {
- logOut(client);
- }
- // Send exit request to server
- else if (atoi(choice) == 3) {
- exitProgram(client);
- return 0;
- }
- // Notice the error
- else
- printf("No such option! Please try again!\n");
- }
- // Close socket
- closesocket(client);
- // Terminate Winsock
- WSACleanup();
- return 0;
- }
- // Function to cut n first character of
- // a pointed string
- void chopNChars(char *str, int n) {
- int strLen = strlen(str);
- if (n > strLen)
- return;
- memmove(str, str + n, strLen - n + 1);
- }
- // Funtion to print menu to client console
- void printMenu() {
- printf("--------MENU--------\n");
- printf("1. Login system\n");
- printf("2. Logout\n");
- printf("3. Exit\n");
- printf("---------------------\n");
- }
- // Funtion to send username and password (login) to server
- void logIn(SOCKET client) {
- int ret;
- char userID[] = USER_PREFIX;
- char buff[BUFF_SIZE] = { '\0' };
- // Enter UserID
- printf("USERNAME:");
- gets_s(buff, BUFF_SIZE);
- strcat(userID, buff);
- // Send UserID to server
- ret = send(client, userID, strlen(userID), 0);
- if (ret == SOCKET_ERROR)
- printf("Error! Cannot send message!\nError code: %i\n", WSAGetLastError());
- chopNChars(userID, 4);
- // Receive echo message
- ret = recv(client, buff, BUFF_SIZE, 0);
- buff[ret] = 0;
- if (ret == SOCKET_ERROR) {
- if (WSAGetLastError() == WSAETIMEDOUT)
- printf("Time-out!\n");
- }
- else if (strlen(buff) > 0) {
- // Check received code to know if the username is valid
- if (strcmp(buff, USER_OK) == 0) {
- // UserID has not been authenticated
- int countLeft = 3;
- // Enter password
- printf("PASSWORD:");
- char passwd[] = PASSWORD_PREFIX;
- gets_s(buff, BUFF_SIZE);
- strcat(passwd, buff);
- // Send password to server
- ret = send(client, passwd, strlen(passwd), 0);
- char buff2[BUFF_SIZE] = "\0";
- // Receive echo message
- ret = recv(client, buff2, BUFF_SIZE, 0);
- if (WSAGetLastError() == WSAETIMEDOUT)
- printf("Time-out!\n");
- buff2[ret] = 0;
- // Check if the password is correct
- if (strcmp(buff2, PASS_OK) == 0) {
- printf("Loggin in to the system!\n\n");
- }
- // The password is incorrect, warn user
- else if ((strcmp(buff2, PASS_INCORRECT) == 0) && (countLeft > 0)) {
- countLeft--;
- printf("Wrong password! %d times left!\n", countLeft);
- }
- // The userID has been locked from the system
- else if (strcmp(buff2, USER_DISABLED) == 0) {
- printf("UserID %s is locked! Please contact with admin for support!\n", userID);
- }
- }
- // The userID does not exist
- else if (strcmp(buff, USER_VOID) == 0) {
- printf("UserID not found!\n");
- }
- // The userID has been locked from the system
- else if (strcmp(buff, USER_DISABLED) == 0) {
- printf("UserID %s is locked! Please contact with admin for support!\n", userID);
- }
- }
- }
- // Funtion to send logout request to server
- void logOut(SOCKET client) {
- int ret;
- char option2[BUFF_SIZE];
- ret = send(client, LOGOUT_PREFIX, strlen(LOGOUT_PREFIX), 0);
- if (ret == SOCKET_ERROR)
- printf("Error! Cannot send message!\nError code: %i\n", WSAGetLastError());
- ret = recv(client, option2, BUFF_SIZE, 0);
- if (WSAGetLastError() == WSAETIMEDOUT)
- printf("Time-out!\n");
- option2[ret] = 0;
- // Check if the logout request is success
- if (strcmp(option2, LOGOUT_SUCCESS) == 0) {
- printf("Logging out ... \n\n");
- }
- else if (strcmp(option2, USER_NOT_AUTHENTICATED) == 0) {
- printf("No account has logged in!\n\n");
- }
- }
- // Function to send exit request to server
- void exitProgram(SOCKET client) {
- int ret;
- char option3[BUFF_SIZE];
- ret = send(client, EXIT_PREFIX, strlen(EXIT_PREFIX), 0);
- if (ret == SOCKET_ERROR)
- printf("Error! Cannot send message!\nError code: %i\n", WSAGetLastError());
- ret = recv(client, option3, BUFF_SIZE, 0);
- if (WSAGetLastError() == WSAETIMEDOUT)
- printf("Time-out!\n");
- option3[ret] = 0;
- if (strcmp(option3, EXIT_OK) == 0)
- printf("Exitting ... \n\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment