#include <iostream>
#include "afxsock.h"
#include "math.h"
#include<string>
#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
//Library initialization
if (AfxSocketInit() == false)
{
cout << "Can not initialize socket library";
return false;
}
CSocket client;
//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 ClientMsg[100];
int MsgSize;
char* temp;
//receive message from server
client.Receive((char*)&MsgSize, sizeof(int), 0);
temp = new char[MsgSize + 1];
client.Receive((char*)temp, MsgSize, 0);
//print the message
temp[MsgSize] = '\0';
cout << "\nServer: " << temp;
//Send command q/l/r
cin.getline(ClientMsg, 100, '\n');
//Wrong command
while (strcmp(ClientMsg, "3") != 0 && strcmp(ClientMsg, "quit") != 0 && strcmp(ClientMsg, "login") != 0 && strcmp(ClientMsg, "1") != 0 && strcmp(ClientMsg, "2") != 0 && strcmp(ClientMsg, "register") != 0) {
cout << "Wrong command" << endl;
cout << "Retype: ";
cin.getline(ClientMsg, 100, '\n');
}
MsgSize = strlen(ClientMsg);
//Send the message's length to server
client.Send(&MsgSize, sizeof(MsgSize), 0);
//Send the message with length is MsgSize
client.Send(ClientMsg, MsgSize, 0);
// goi chieu dai chuoi
char buff[10];
if (strcmp(ClientMsg, "3") == 0 || strcmp(ClientMsg, "quit") == 0) {
client.Close();
return nRetCode;
}
else if (strcmp(ClientMsg, "1") == 0 || strcmp(ClientMsg, "login") == 0) {
int check = 0;
bool success = false;
do {
delete temp;
//Receiving USERNAME from server
client.Receive((char*)&MsgSize, sizeof(int), 0);
temp = new char[MsgSize + 1];
client.Receive((char*)temp, MsgSize, 0);
temp[MsgSize] = '\0';
cout << temp;
//Send username
char username[1000];
cin.getline(username, 100, '\n');
MsgSize = strlen(username);
//Send the message's length to server
client.Send(&MsgSize, sizeof(MsgSize), 0);
//Send the message with length is MsgSize
client.Send(username, MsgSize, 0);
delete temp;
//Receiving PASSWORD from server
client.Receive((char*)&MsgSize, sizeof(int), 0);
temp = new char[MsgSize + 1];
client.Receive((char*)temp, MsgSize, 0);
temp[MsgSize] = '\0';
cout << temp;
//Send password
char password[1000];
//Enter Pass
cin.getline(password, 100, '\n');
MsgSize = strlen(password);
//Send the message's length to server
client.Send(&MsgSize, sizeof(MsgSize), 0);
//Send the message with length is MsgSize
client.Send(password, MsgSize, 0);
_itoa_s(strlen(password), buff, 10);
//nhan ket qua
delete temp;
//Receiving PASSWORD from server
client.Receive((char*)&MsgSize, sizeof(int), 0);
temp = new char[MsgSize + 1];
client.Receive((char*)temp, MsgSize, 0);
temp[MsgSize] = '\0';
if (strcmp(temp, "Invalid account") == 0) {
cout << temp << endl;
check++;
cout << 3 - check << " chances left" << endl;
}
else {
cout << temp;
check = 3;
success = true;
}
} while (strcmp(temp, "Invalid account") == 0 && check != 3);
if (success) {
char command[1000];
cin.getline(command, 100, '\n');
MsgSize = strlen(command);
//Send the message's length to server
client.Send(&MsgSize, sizeof(MsgSize), 0);
//Send the message with length is MsgSize
client.Send(command, MsgSize, 0);
}
else {
cout << "UNSUCCESSFULL to login" << endl;
}
client.Close();
return nRetCode;
}
}
else {
printf("Can not connect to server!");
}
getchar();
client.Close();
}
getchar();
return nRetCode;
}