#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 << endl; 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); //username 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); //password client.Receive(r_str, 10, 0); len = atoi(r_str); client.Receive(r_str, len, 0); r_str[len] = 0; cout << r_str; 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; client.Close(); } else { printf("Can not connect to server!"); } getchar(); client.Close(); } getchar(); return nRetCode; }