Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:11:17+00:00 2026-06-17T21:11:17+00:00

I have an ugly C code (I’m newbie in C under Windows) don’t judge

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:11:19+00:00Added an answer on June 17, 2026 at 9:11 pm

    See the MSDN docs for beginthreadex. You need to change the signature of isAlive to

    unsigned int _stdcall isAlive(void *ptr)
    

    _stdcall specifies a different calling convention. See here for a list of the available options.

    You can retrieve your addr argument simply by casting ptr:

    char* addr = (char*)ptr;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code, and I think it is ugly: loginCheck = do
I have a ugly piece of code that adds event handlers. The problem is,
I am new to rails so beware of the ugly code. I have these
I have this ugly piece of code.. orders = got_something_from_db() if orders: for order
I have the following ugly hard code: <div class=label> <p id=CO-0></p> <p id=CO-1></p> <p
I have this ugly code: if ( v > 10 ) size = 6;
I have a ugly code that can't be reused. I have many similar queries.
I have this ugly SELECT statement : Select 'Urbain' as Véhicule, Count( DISTINCT Plaintes.IDEvenements)
I have an ugly druple-gardens RSS feed coming into flash and I'm using the
I have the following ugly if statement that is part of a class that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.