I have an ugly C code (I’m newbie in C under Windows) don’t judge me…I wat to make it multithreading, it reads from a list of my webservers and check if them are alive, for fastness I need it to be multithreaded. Searched a lot of google and howtos and the best I could do is as follows:
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <winsock.h>
#include <process.h>
#include <string.h>
#pragma comment(lib, "wsock32.lib")
int isAlive(char *addr)
{
struct sockaddr_in blah;
struct hostent *he;
WSADATA wsaData;
int i;
WORD wVersionRequested;
SOCKET sock;
char buff[1024];
char *ex;
ex="GET /alive.php HTTP/1.0\n\n";
char *fmsg="ALIVE";
wVersionRequested = MAKEWORD(1, 1);
if (WSAStartup(wVersionRequested , &wsaData)){
printf("Winsock Initialization failed.\n");
return(1);
}
if ((sock=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET){
printf("Can not create socket.\n");
return(1);
}
sock = socket(AF_INET,SOCK_STREAM,0);
blah.sin_family = AF_INET;
blah.sin_port = htons(80);
blah.sin_addr.s_addr = inet_addr(addr);
if ((he=gethostbyname(addr))!=NULL){
memcpy((char *)&blah.sin_addr.s_addr,he->h_addr,he->h_length);
}
else{
if((blah.sin_addr.s_addr=inet_addr(addr))==-1){
WSACleanup();
return(1);
}
}
if (connect(sock,(struct sockaddr*)&blah,sizeof(blah))==0){
send(sock,ex,strlen(ex),0);
recv(sock,buff,sizeof(buff),0);
if(strstr(buff,fmsg)!=NULL){
printf("ALIVE: %s", addr);
}
}
closesocket(sock);
WSACleanup();
_endthreadex(0);
return(1);
}
int main(int argc,char *argv[])
{
if(argc!=2){
printf("Usage: scan <webservers list>\n");
return(1);
}
char *inname = argv[1];
FILE *infile;
char line_buffer[BUFSIZ];
char line_number;
int numconnect;
infile = fopen(inname, "r");
if (!infile) {
printf("Couldn't open file %s for reading.\n", inname);
return 0;
}
line_number = 0;
numconnect=20;
while (fgets(line_buffer, sizeof(line_buffer), infile)) {
++line_number;
//printf("%s", line_buffer);
unsigned x;
unsigned tempThreadID;
for(x=0;x<numconnect;++x){
_beginthreadex(0, 0, isAlive, line_buffer, 0, &tempThreadID);
}
}
return 0;
}
When I try to compile under VC 10 I get the following error:
checkalive.c(90) : error C2664: ‘beginthreadex’ : cannot convert
parameter 3 from ‘int (_cdecl *)(char *)’ to ‘unsigned int (__stdcall
*)(void *)’
Thanks for help!
See the MSDN docs for beginthreadex. You need to change the signature of
isAliveto_stdcallspecifies a different calling convention. See here for a list of the available options.You can retrieve your
addrargument simply by castingptr: