gpt4 book ai didi

C winsock 和 _beginthreadex 编译错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:58:10 26 4
gpt4 key购买 nike

我有一个丑陋的 C 代码(我是 Windows 下 C 的新手)不要评判我......我想让它成为多线程,它从我的网络服务器列表中读取并检查它们是否存在,因为牢度我需要它是多线程的。搜索了很多谷歌和 howtos,我能做的最好的如下:

#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;
}

当我尝试在 VC 10 下编译时,出现以下错误:

checkalive.c(90) : error C2664: 'beginthreadex' : cannot convert parameter 3 from 'int (_cdecl *)(char *)' to 'unsigned int (__stdcall *)(void *)'

感谢您的帮助!

最佳答案

请参阅 beginthreadex 的 MSDN 文档.您需要将 isAlive 的签名更改为

unsigned int _stdcall isAlive(void *ptr)

_stdcall 指定不同的调用约定。参见 here获取可用选项的列表。

您可以简单地通过转换 ptr 来检索您的 addr 参数:

char* addr = (char*)ptr;

关于C winsock 和 _beginthreadex 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14547546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com