gpt4 book ai didi

c++ - gethostbyname 创建一个线程?

转载 作者:太空狗 更新时间:2023-10-29 21:08:28 31 4
gpt4 key购买 nike

我在 C++ 中使用 VS2008 和 Win7。

在检查程序时,我一直在关注创建的线程,gethostbyname() 似乎为自己创建了一个线程。你能解释一下为什么吗?

在 msdn 上说:“由 gethostbyname 函数返回的主机结构的内存由 Winsock DLL 从线程本地存储内部分配。”

这个内存是否让 visual studio 误以为它是一个线程?

编辑:似乎来自 this link ,并且根据我的观察,连接函数也会发生这种情况。我想这是正常行为。

下面的代码来自 msdn [gethostbyname page],它表现出相同的行为。

int main(int argc, char **argv)    
{
//-----------------------------------------
// Declare and initialize variables
WSADATA wsaData;
int iResult;

DWORD dwError;
int i = 0;

struct hostent *remoteHost;
char *host_name;
struct in_addr addr;

char **pAlias;

// Validate the parameters
if (argc != 2) {
printf("usage: %s hostname\n", argv[0]);
printf(" to return the IP addresses for the host\n");
printf(" %s www.contoso.com\n", argv[0]);
printf(" or\n");
printf(" %s IPv4string\n", argv[0]);
printf(" to return an IPv4 binary address for an IPv4string\n");
printf(" %s 127.0.0.1\n", argv[0]);
return 1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}

host_name = argv[1];

printf("Calling gethostbyname with %s\n", host_name);
remoteHost = gethostbyname(host_name);

if (remoteHost == NULL) {
dwError = WSAGetLastError();
if (dwError != 0) {
if (dwError == WSAHOST_NOT_FOUND) {
printf("Host not found\n");
return 1;
} else if (dwError == WSANO_DATA) {
printf("No data record found\n");
return 1;
} else {
printf("Function failed with error: %ld\n", dwError);
return 1;
}
}
} else {
printf("Function returned:\n");
printf("\tOfficial name: %s\n", remoteHost->h_name);
for (pAlias = remoteHost->h_aliases; *pAlias != 0; pAlias++) {
printf("\tAlternate name #%d: %s\n", ++i, *pAlias);
}
printf("\tAddress type: ");
switch (remoteHost->h_addrtype) {
case AF_INET:
printf("AF_INET\n");
break;
case AF_NETBIOS:
printf("AF_NETBIOS\n");
break;
default:
printf(" %d\n", remoteHost->h_addrtype);
break;
}
printf("\tAddress length: %d\n", remoteHost->h_length);

i = 0;
if (remoteHost->h_addrtype == AF_INET)
{
while (remoteHost->h_addr_list[i] != 0) {
addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
printf("\tIP Address #%d: %s\n", i, inet_ntoa(addr));
}
}
else if (remoteHost->h_addrtype == AF_NETBIOS)
{
printf("NETBIOS address was returned\n");
}
}
return 0;
}

最佳答案

据我所知,gethostbyname block 。

虽然 WinSock 经常创建一些辅助线程。

关于c++ - gethostbyname 创建一个线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2976654/

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