#include #include "afxsock.h" #include "math.h" #include #include #ifdef _DEBUG #define new DEBUG_NEW #endif #define Saddress "127.0.0.1" #define port 8080 //Same port as server CWinApp theApp; using namespace std; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; //initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, GetCommandLine(), 0)) { //TODO: change error code to suit your needs _tprintf(_T("Fatal error: MFC initialization failed\n")); nRetCode = 1; } else { //TODO: code your application's behaviour here CSocket client; AfxSocketInit(NULL); //1.Create Socket client.Create(); if (client.Connect(CA2W(Saddress), port)) { cout << "Client have connected to Server!" << endl; //Receiving from server the position number int id; client.Receive((char*)&id, sizeof(id), 0); cout << "\n\nThis is client number " << id + 1 << endl << endl; char r_str[1000], s_str[1000]; //Receiving returning results from server client.Receive(r_str, 10, 0); int len = atoi(r_str); client.Receive(r_str, len, 0); r_str[len] = 0; cout << "\nServer: " << r_str; //Send command q/l/r gets_s(s_str); //Wrong command while (strcmp(s_str, "3") != 0 && strcmp(s_str, "quit") != 0 && strcmp(s_str, "login") != 0 && strcmp(s_str, "1") != 0 && strcmp(s_str, "2") != 0 && strcmp(s_str, "register") != 0) { cout << "Wrong command" << endl; cout << "Retype: "; gets_s(s_str); } // goi chieu dai chuoi char buff[10]; _itoa_s(strlen(s_str), buff, 10); client.Send(buff, 10, 0); //gui chuoi client.Send(s_str, strlen(s_str), 0); if (strcmp(s_str, "3") == 0 || strcmp(s_str, "quit") == 0) { client.Close(); return nRetCode; } else if (strcmp(s_str, "1") == 0 || strcmp(s_str, "login") == 0) { int check = 0; bool success = false; do { //username client.Receive(r_str, 10, 0); len = atoi(r_str); client.Receive(r_str, len, 0); r_str[len] = 0; cout << r_str; //Send username char username[1000]; gets_s(username); // goi chieu dai chuoi _itoa_s(strlen(username), buff, 10); client.Send(buff, 10, 0); //gui chuoi client.Send(username, strlen(username), 0); // Nhan password client.Receive(r_str, 10, 0); len = atoi(r_str); client.Receive(r_str, len, 0); r_str[len] = 0; cout << r_str; //Send password char password[1000]; gets_s(password); // goi chieu dai chuoi _itoa_s(strlen(password), buff, 10); client.Send(buff, 10, 0); //gui chuoi client.Send(password, strlen(password), 0); //nhan ket qua client.Receive(r_str, 10, 0); len = atoi(r_str); client.Receive(r_str, len, 0); r_str[len] = 0; if (strcmp(r_str, "Invalid account") == 0) { cout << r_str << endl; check++; cout << 3 - check << " chances left" << endl; } else { cout << r_str; check = 3; success = true; } } while (strcmp(r_str, "Invalid account") == 0 && check != 3); if (success) { char command[1000]; gets_s(command); _itoa_s(strlen(command), buff, 10); client.Send(buff, 10, 0); //gui chuoi client.Send(command, strlen(command), 0); } else { cout << "UNSUCCESSFULL to login" << endl; } client.Close(); return nRetCode; } else if (strcmp(s_str, "2") == 0 || strcmp(s_str, "register") == 0) { client.Receive(r_str, 10, 0); len = atoi(r_str); client.Receive(r_str, len, 0); r_str[len] = 0; cout << r_str; char username[1000]; gets_s(username); _itoa_s(strlen(username), buff, 10); client.Send(buff, 10, 0); //gui chuoi client.Send(username, strlen(username), 0); client.Receive(r_str, 10, 0); len = atoi(r_str); client.Receive(r_str, len, 0); r_str[len] = 0; cout << r_str; while (strcmp(r_str, "Invalid username\n This username has already been used\n Proceed to input another username\n") == 0) { client.Receive(r_str, 10, 0); len = atoi(r_str); client.Receive(r_str, len, 0); r_str[len] = 0; cout << r_str; char username[1000]; gets_s(username); // goi chieu dai chuoi _itoa_s(strlen(username), buff, 10); client.Send(buff, 10, 0); //gui chuoi client.Send(username, strlen(username), 0); client.Receive(r_str, 10, 0); len = atoi(r_str); client.Receive(r_str, len, 0); r_str[len] = 0; cout << r_str; } // Nhan password client.Receive(r_str, 10, 0); len = atoi(r_str); client.Receive(r_str, len, 0); r_str[len] = 0; cout << r_str; //Send password char password[1000]; gets_s(password); // goi chieu dai chuoi _itoa_s(strlen(password), buff, 10); client.Send(buff, 10, 0); //gui chuoi client.Send(password, strlen(password), 0); } } else { printf("Can not connect to server!"); } getchar(); client.Close(); } getchar(); return nRetCode; }