C语言 WSAStartup 函数如何启动 Winsock DLL 的使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4991967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:47:34  来源:igfitidea点击:

How does WSAStartup function initiates use of the Winsock DLL?

cwinapinetwork-programmingwinsockwsastartup

提问by Searock

How does WSAStartup function initiates use of the Winsock DLL?

WSAStartup 函数如何启动 Winsock DLL 的使用?

According to the documentation

根据文档

The WSAStartup function must be the first Windows Sockets function called by an application or DLL. It allows an application or DLL to specify the version of Windows Sockets required and retrieve details of the specific Windows Sockets implementation. The application or DLL can only issue further Windows Sockets functions after successfully calling WSAStartup.

WSAStartup 函数必须是应用程序或 DLL 调用的第一个 Windows 套接字函数。它允许应用程序或 DLL 指定所需的 Windows 套接字版本并检索特定 Windows 套接字实现的详细信息。应用程序或 DLL 只能在成功调用 WSAStartup 后发出更多的 Windows 套接字函数。

This function initializes WSADATAdata structure, but in socket programming we don't pass WSDATAto any function so how does the program comes to know about the Windows Sockets version and other details?

这个函数初始化WSADATA数据结构,但在套接字编程中我们不传递WSDATA给任何函数,那么程序如何知道 Windows 套接字版本和其他细节?

For example in this code

例如在这段代码中

#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32")

void Run(int argc, char* argv[])
{
    char* host = argc < 2 ? "" : argv[1];
    struct hostent* entry = gethostbyname(host);

    if(entry)
    {
        struct in_addr* addr = (struct in_addr*) entry->h_addr;
        printf("IP Address: %s\n", inet_ntoa(*addr));
    }
    else
        printf("ERROR: Resolution failure.\n");
}

int main(int argc, char* argv[])
{
    WSADATA wsaData;

    if(WSAStartup(0x202, &wsaData) == 0)
    {
        Run(argc, argv);
        WSACleanup();
    }
    else
        printf("ERROR: Initialization failure.\n");
}

In this example I am initializing WSADATAdata structure using WSAStartup()function and after wards I'm not passing wsaDataanywhere.

在这个例子中,我WSADATA使用WSAStartup()函数初始化数据结构,之后我没有通过wsaData任何地方。

So how does my program comes to know about wsaDatadetails?

那么我的程序是如何知道wsaData细节的呢?

Thanks.

谢谢。

回答by Gavin Lock

WSAStartup has two main purposes.

WSAStartup 有两个主要目的。

Firstly, it allows you to specify what version of WinSock you want to use (you are requesting 2.2 in your example). In the WSADATA that it populates, it will tell you what version it is offering you based on your request. It also fills in some other information which you are not required to look at if you aren't interested. You never have to submit this WSADATA struct to WinSock again, because it is used purely to give you feedback on your WSAStartup request.

首先,它允许您指定要使用的 WinSock 版本(您在示例中请求的是 2.2)。在它填充的 WSADATA 中,它会根据您的请求告诉您它为您提供的版本。它还填写了一些其他信息,如果您不感兴趣,则不需要查看这些信息。您永远不必再次向 WinSock 提交这个 WSADATA 结构,因为它纯粹用于为您提供有关 WSAStartup 请求的反馈。

The second thing it does, is to set-up all the "behind the scenes stuff" that your app needs to use sockets. The WinSock DLL file is loaded into your process, and it has a whole lot of internal structures that need to be set-up for each process. These structures are hidden from you, but they are visible to each of the WinSock calls that you make.

它所做的第二件事是设置您的应用程序需要使用套接字的所有“幕后内容”。WinSock DLL 文件被加载到你的进程中,它有很多内部结构需要为每个进程设置。这些结构对您是隐藏的,但对您进行的每个 WinSock 调用都是可见的。

Because these structures need to be set-up for each process that uses WinSock, each process must call WSAStartup to initialise the structures within its own memory space, and WSACleanup to tear them down again, when it is finished using sockets.

因为需要为每个使用 WinSock 的进程设置这些结构,所以每个进程必须调用 WSAStartup 来初始化它自己内存空间中的结构,并调用 WSACleanup 在它使用完套接字后再次拆除它们。