(2/2)
Chiedo venia per il disordine immane del codice....
Purtroppo oggi non funzia neppure la ricerca
Se potete indicarmi anche link di rifrimento vi sarei grato...
codice:
// File ControlThread.cpp
#include <iostream>
#include <winsock2.h>
#define KEY_ESC 0x1b
using namespace std;
const char strLogin[]="Mio server di prova\nPremi ESC per disconnetterti\n\r";
const char strCommandReply[]=": Comando non ancora implementato\n\r";
void sSay(LPVOID sock, const char * string)
{
send(*(SOCKET *)sock, &string[0], strlen(string), 0);
}
void ExecCommand(const int pid, char * command, LPVOID sock, int &index)
{
cout << pid << ": manda il comando '" << command << "'" << endl;
sSay(sock, strcat(command,strCommandReply));
index=0;
}
DWORD WINAPI controlla(LPVOID sock)
{
int pid, err, CommandIndex = 0;
char rec, strCommand[100];
strCommand[1] = '\0';
pid=GetCurrentThreadId();
cout << "Nuova connessione aperta: " << pid << endl;
cout << pid << ": Invio i dati al client..." << endl;
sSay(sock, &strLogin[0]);
while(rec != KEY_ESC)
{
err = recv(*(SOCKET *)sock, &rec ,1,0);
if (err == SOCKET_ERROR)
{
cerr << pid << ": Errore Socket: " << WSAGetLastError() << endl;
return 1;
}
// copia nella strCommand
if (rec != 13)
{
if (rec != 10)
{
strCommand[CommandIndex++] = rec;
strCommand[CommandIndex+1] = '\0';
}
}
else
{
rec = ' ';
ExecCommand(pid, strCommand,sock,CommandIndex);
}
}
cout << pid << ": Utente Disconnesso" << endl;
shutdown(*(SOCKET *)sock, 2);
return 0;
}