1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
void abc(CSocket sockClients[],int i)
{
int MsgSize;
char *temp;
// Nhan username tu Server
sockClients[i].Receive((char*)&MsgSize,sizeof(int),0); // Neu nhan loi thi tra ve la SOCKET_ERROR.
temp = new char[MsgSize +1];
sockClients[i].Receive((char*)temp,MsgSize,0);
// In username ra
temp[MsgSize] ='\0';
int score=3;
int ques;
sockClients[i].Receive((char*)&ques,sizeof(int),0);
ifstream CH;
CH.open("cauhoi.txt",ios::in);
ifstream DA;
DA.open("dapan.txt",ios::in);
for(int j=0;j<ques;j++)
{
char* CH_SV=new char;
CH>>CH_SV;
int Size = strlen(CH_SV);
sockClients[i].Send(&Size,sizeof(int),0);
sockClients[i].Send(CH_SV,Size,0);
char* DA_SV=new char;
DA>>DA_SV;
char *dapan;
sockClients[i].Receive((char*)&Size,sizeof(int),0);
dapan = new char[Size +1];
sockClients[i].Receive((char*)dapan,Size,0);
dapan[Size] ='\0';
cout<<"\nDap an la: "<<dapan<<endl;
if(Size<5)
goto Exit;
else if(strcmp(dapan,DA_SV)==0)
score+=2;
else score--;
}
sockClients[i].Close();
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, 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 behavior here.
CSocket server;
AfxSocketInit(NULL);
server.Create(3456);
server.Listen();
int nclient;
cout<<"Nhap so luong client: ";
cin>>nclient;
CSocket * sockClients = new CSocket[nclient];
for (int i = 0; i <nclient; i++)
{
server.Accept(sockClients[i]);
printf("\nClient: %d/%d",i+1,nclient);
sockClients[i].Send((char*)&i,sizeof(int),0);
abc(sockClients,i);
}
server.Close();
}
}
else
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}
return nRetCode;
}
|