When I tried to do the same in my code I got a error like
Error 1 error C2664: 'CreateThread' : cannot convert parameter 3 from 'DWORD (__stdcall AcceptSocket::* )(LPVOID)' to 'LPTHREAD_START_ROUTINE' D:\Work\Chess\Chess\Chess\NetWorking.cpp 65 1 Chess
I am posting my code
First the header file
#include<winsock.h>
#include<string>
#include<iostream>
using namespace std;
class AcceptSocket
{
static SOCKET s;
protected:
SOCKET acceptSocket;
public:
AcceptSocket(){};
void setSocket(SOCKET socket);
static void EstablishConnection(int portNo,string&);
static void closeConnection();
static void StartAccepting();
virtual void threadDeal();
DWORD WINAPI MyThreadFunction(LPVOID lpParam);
};
Now the source file
#include<NetWorking.h>
#include<string>
#include<fstream>
#include<sstream>
SOCKET AcceptSocket::s;
void AcceptSocket::setSocket(SOCKET s)
{
acceptSocket=s;
}
void AcceptSocket::EstablishConnection(int portno,string &failure)
{
WSAData w;
int error = WSAStartup(0x0202,&w);
if(error)
failure=failure+"\nWSAStartupFailure";
if(w.wVersion != 0x0202)
{
WSACleanup();
failure=failure+"\nVersion is different";
}
SOCKADDR_IN addr;
addr.sin_family=AF_INET;
addr.sin_port=htons(portno);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
AcceptSocket::s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(AcceptSocket::s == INVALID_SOCKET)
failure=failure+"\nsocket creating error";
if(bind(AcceptSocket::s,(LPSOCKADDR) &addr,sizeof(addr)) == SOCKET_ERROR)
failure=failure+"\nbinding error";
listen(AcceptSocket::s,SOMAXCONN);
}
void AcceptSocket::closeConnection()
{
if(AcceptSocket::s)
closesocket(AcceptSocket::s);
WSACleanup();
}
void AcceptSocket::StartAccepting()
{
sockaddr_in addrNew;
int size=sizeof(addrNew);
while(1)
{
SOCKET temp=accept(AcceptSocket::s,(sockaddr *)&addrNew,&size);
AcceptSocket * tempAcceptSocket=new AcceptSocket();
tempAcceptSocket->setSocket(temp);
DWORD threadId;
cout << "Before the thread" << endl ;
HANDLE thread=CreateThread(NULL,0,&AcceptSocket::MyThreadFunction,(LPVOID)(tempAcceptSocket),0,&threadId);
cout << GetLastError() << endl ;
}
}
DWORD WINAPI AcceptSocket::MyThreadFunction(LPVOID lpParam)
{
AcceptSocket * acceptsocket=(AcceptSocket *) lpParam;
acceptsocket->threadDeal();
return 1;
}
Now the main
#include<Networking.h>
int main()
{
}
I don't how to correct this. Can anybody help me?