WSAStartup 函数如何启动 Winsock DLL 的使用? [英] How does WSAStartup function initiates use of the Winsock DLL?

查看:12
本文介绍了WSAStartup 函数如何启动 Winsock DLL 的使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How does WSAStartup function initiates use of the Winsock DLL?

根据文档

WSAStartup 函数必须是第一个调用的 Windows Sockets 函数通过应用程序或 DLL.它允许一个应用程序或 DLL 来指定所需的 Windows 套接字版本并检索特定的详细信息Windows 套接字实现.这应用程序或 DLL 只能发出进一步的 Windows Sockets 函数成功调用 WSAStartup 后.

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.

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

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

例如在这段代码中

#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
", inet_ntoa(*addr));
    }
    else
        printf("ERROR: Resolution failure.
");
}

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

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

在此示例中,我使用 WSAStartup() 函数初始化 WSADATA 数据结构,之后我不会在任何地方传递 wsaData.

In this example I am initializing WSADATA data structure using WSAStartup() function and after wards I'm not passing wsaData anywhere.

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

So how does my program comes to know about wsaData details?

谢谢.

推荐答案

WSAStartup 有两个主要目的.

WSAStartup has two main purposes.

首先,它允许您指定要使用的 WinSock 版本(您在示例中请求的是 2.2).在它填充的 WSADATA 中,它将根据您的请求告诉您它为您提供的版本.它还填写了一些其他信息,如果您不感兴趣,则不需要查看这些信息.您无需再次向 WinSock 提交此 WSADATA 结构,因为它纯粹用于向您提供有关您的 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 DLL 文件被加载到您的进程中,并且它具有需要为每个进程设置的大量内部结构.这些结构对您隐藏,但对您进行的每个 WinSock 调用都是可见的.

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 的进程设置这些结构,所以每个进程必须调用 WSAStartup 以在其自己的内存空间中初始化这些结构,并在使用完套接字后再次调用 WSACleanup 将它们拆除.

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.

这篇关于WSAStartup 函数如何启动 Winsock DLL 的使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆