Hello everybody!
It's the first time that I write on this forum. I'm still a beginner on C++ world (i'm coming from Java, .NET and C#, this is the first experience with low level programming languages).
Lately I was trying to use C++ with classes (yeah... really hard to begin), then I decided to start writing a simple TCP multithread server.
It's quite all clear what i have to do, but there still something that i cannot understand with threads as shown in the code below:
1 2 3 4 5 6 7 8 9 10 11
|
void Srv::start()
{
bool stop;
thread tServer(serverThread, ref(stop));
}
void Srv::serverThread(bool param)
{
//server's BL
}
|
I have a public class named 'Srv' within it there are quite lot of utilities methods called within 'serverThread()'. In 'start()' function, on the tServer's constructor declaration line, I get this error: "No instance of constructor "std::thread::thread" matches the argument list. Arugment type are (void (bool param))". If i try to put a simple method like...
1 2 3 4
|
void hello()
{
//hello's BL
}
|
...and it's all right.
The goal that i want to reach is to have one function who invoke the server.
Sorry for my 'newbie incompetence', probably it could be solved in a few seconds, but I'm still learning and it's not simple for me now :)
I'll also provide server's BL if could be for help (don't care about italian words within cout and cerr, nothing offensive :) ):
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
|
void Srv::serverThread(bool param)
{
LPDWORD thread = NULL;
struct sockaddr_in srvAddr, client;
srvAddr.sin_family = AF_INET;
srvAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
srvAddr.sin_port = 27015;
//getSck is a wrapper that simply return a SOCKET... nothing special...
SOCKET sck = getSck(&srvAddr);
int clientLength = sizeof(client);
char clBuffer[1024];
//--------------------------------------------------------------------------
while (param){
SOCKET clientAccepted = accept(sck, (sockaddr*)&client, &clientLength);
if (clientAccepted == INVALID_SOCKET){
cerr << "accept fallita con codice di errore: %ld\n";
cerr << WSAGetLastError();
closesocket(clientAccepted);
WSACleanup();
}
else {
cout << "Client connesso" << endl;
}
//ha-ha! Another similar error here on 'reciver' callback,
//but i'm confident to find other ways!
CreateThread(NULL, 0, reciver, (LPVOID)clientAccepted, 0, thread);
}
//-------------------------------------------------------------------------
cout << "Spegnimento" << endl;
int sckClosed = closesocket(sck);
if (sckClosed == SOCKET_ERROR)
{
cout << "Non spento" << endl;
}
else
{
cout << "Spento" << endl;
WSACleanup();
}
ExitThread(0);
return NULL;
}
|
...and class header file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
class Srv
{
private:
pthread_t server, cmd;
SOCKET getSck(sockaddr_in *srv);
DWORD WINAPI reciver(LPVOID param);
void serverThread(bool param); //<----- 1
void cmdThread(bool param);
public:
Srv();
Srv(pthread_t server, pthread_t cmd);
void start(); //<------ 2
pthread_t Srv::getCmdThread();
void Srv::setCmdThread(pthread_t cmd);
pthread_t getServerThread();
void setServerThread(pthread_t server);
};
|
Will i provided all the necessary?
Hope someone will help a poor and hangry for C++ guy :)
Thankyou!
Regards,
Andrea