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

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

问题描述

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

How does WSAStartup function initiates use of the Winsock DLL?

根据文档

的调用WSAStartup函数必须是
  首先调用Windows套接字功能
  由应用程序或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?

例如,在此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");
}

在这个例子中,我使用的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.

首先,它允许您指定要使用(您要求2.2在你的例子)是什么版本的Winsock。在它填充WSADATA,它会告诉你什么版本是根据您的要求为您提供。这也填补了其他的一些信息的您不需要看,如果你不感兴趣的。你永远不必再次提交这个WSADATA结构来的WinSock,因为它是用来纯粹是为了给你对你的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天全站免登陆