#include #include "afxsock.h" #include "math.h" #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, "q") != 0 && strcmp(s_str, "quit") != 0 && strcmp(s_str, "login") != 0 && strcmp(s_str, "l") != 0 && strcmp(s_str, "r") != 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, "q") == 0 || strcmp(s_str, "quit") == 0) { client.Close(); return nRetCode; } else { //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; cout << r_str; 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); client.Close(); return nRetCode; } } else { printf("Can not connect to server!"); } getchar(); client.Close(); } getchar(); return nRetCode; }