WSAStartup function

WSAStartup function

Parameters

wVersionRequested [in]

The highest version of Windows Sockets specification that the caller can use. The high-order byte specifies the minor version number; the low-order byte specifies the major version number.

lpWSAData [out]

A pointer to the WSADATA data structure that is to receive details of the Windows Sockets implementation.

Return value

If successful, the WSAStartup function returns zero. Otherwise, it returns one of the error codes listed below.

The WSAStartup function directly returns the extended error code in the return value for this function. A call to the WSAGetLastError function is not needed and should not be used.

Error code Meaning
WSA SYS NOTREADY

The underlying network subsystem is not ready for network communication.

WSA VER NOT SUPPORTED

The version of Windows Sockets support requested is not provided by this particular Windows Sockets implementation.

WSA E IN PROGRESS

A blocking Windows Sockets 1.1 operation is in progress.

WSA E PROC LIM

A limit on the number of tasks supported by the Windows Sockets implementation has been reached.

WSA E FAULT

The lpWSAData parameter is not a valid pointer.

Remarks

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.

In order to support various Windows Sockets implementations and applications that can have functional differences from the latest version of Windows Sockets specification, a negotiation takes place in WSAStartup. The caller of WSAStartup passes in the wVersionRequested parameter the highest version of the Windows Sockets specification that the application supports. The Winsock DLL indicates the highest version of the Windows Sockets specification that it can support in its response. The Winsock DLL also replies with version of the Windows Sockets specification that it expects the caller to use.

wVersionRequested指明程序支持的最高版本,winsock返回其支持的最高版本,以及返回winsock期望程序使用的版本。

When an application or DLL calls the WSAStartup function, the Winsock DLL examines the version of the Windows Sockets specification requested by the application passed in the wVersionRequested parameter. If the version requested by the application is equal to or higher than the lowest version supported by the Winsock DLL, the call succeeds and the Winsock DLL returns detailed information in the WSADATA structure pointed to by thelpWSAData parameter. The wHighVersion member of the WSADATA structure indicates the highest version of the Windows Sockets specification that the Winsock DLL supports. The wVersion member of the WSADATA structure indicates the version of the Windows Sockets specification that the Winsock DLL expects the caller to use.

The current version of the Windows Sockets specification is version 2.2. The current Winsock DLL, Ws2_32.dll, supports applications that request any of the following versions of Windows Sockets specification:

  • 1.0
  • 1.1
  • 2.0
  • 2.1
  • 2.2

To get full access to the new syntax of a higher version of the Windows Sockets specification, the application must negotiate for this higher version. In this case, the wVersionRequested parameter should be set to request version 2.2. The application must also fully conform to that higher version of the Windows Socket specification, such as compiling against the appropriate header file, linking with a new library, or other special cases. The Winsock2.h header file for Winsock 2 support is included with the Microsoft Windows Software Development Kit (SDK).

2015年的今天,所有PC已全面支持winsock2.2。

It is legal and possible for an application or DLL written to use a lower version of the Windows Sockets specification that is supported by the Winsock DLL to successfully negotiate this lower version using the WSAStartup function. For example, an application can request version 1.1 in the wVersionRequested parameter passed to the WSAStartup function on a platform with the Winsock 2.2 DLL. In this case, the application should only rely on features that fit within the version requested. New Ioctl codes, new behavior of existing functions, and new functions should not be used. The version negotiation provided by the WSAStartup was primarily used to allow older Winsock 1.1 applications developed for Windows 95 and Windows NT 3.51 and earlier to run with the same behavior on later versions of Windows. The Winsock.h header file for Winsock 1.1 support is included with the Windows SDK.

This negotiation in the WSAStartup function allows both the application or DLL that uses Windows Sockets and the Winsock DLL to support a range of Windows Sockets versions. An application or DLL can use the Winsock DLL if there is any overlap in the version ranges. Detailed information on the Windows Sockets implementation is provided in the WSADATA structure returned by the WSAStartup function.

Once an application or DLL has made a successful WSAStartup call, it can proceed to make other Windows Sockets calls as needed. When it has finished using the services of the Winsock DLL, the application must call WSACleanup to allow the Winsock DLL to free internal Winsock resources used by the application.

An application can call WSAStartup more than once if it needs to obtain the WSADATA structure information more than once. On each such call, the application can specify any version number supported by the Winsock DLL.

The WSAStartup function typically leads to protocol-specific helper DLLs being loaded. As a result, the WSAStartup function should not be called from the DllMain function in a application DLL. This can potentially cause deadlocks. For more information, please see the DLL Main Function.

不要在Dll Main中调用WASSartup。

An application must call the WSACleanup function for every successful time the WSAStartup function is called. This means, for example, that if an application calls WSAStartup three times, it must call WSACleanup three times. The first two calls to WSACleanup do nothing except decrement an internal counter; the final WSACleanup call for the task does all necessary resource deallocation for the task.

Startup、Cleanup一一对应。

Note  An application can call the WSAGetLastError function to determine the extended error code for other Windows sockets functions as is normally done in Windows Sockets even if the WSAStartup function fails or the WSAStartup function was not called to properly initialize Windows Sockets before calling a Windows Sockets function. The WSAGetLastError function is one of the only functions in the Winsock 2.2 DLL that can be called in the case of a WSAStartup failure.

参考:https://msdn.microsoft.com/zh-cn/subscriptions/downloads/ms742213(v=vs.85).aspx

时间: 2024-10-08 10:19:13

WSAStartup function的相关文章

WSAStartup()函数的使用

int WSAStartup( __in WORD wVersionRequested, __out LPWSADATA lpWSAData ); WSAStartup 格  式: int PASCAL FAR WSAStartup( WORD wVersionRequested,  LPWSADATA lpWSAData ); 参  数:  wVersionRequested 欲使用的 Windows Sockets API 版本 lpWSAData  指向 WSADATA 资料的指标 传回值

获取IP地址

获取本地计算机的IP地址 关键点 WSAStartup The Windows Sockets WSAStartup function initiates use of Ws2_32.dll by a process. int WSAStartup( WORDwVersionRequested, LPWSADATAlpWSAData ); 实现过程 #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") char* 

c++下基于windows socket的单线程服务器客户端程序

今天自己用编写了一个简单的c++服务器客户端程序,注释较详细,在此做个笔记. windows下socket编程的主要流程可概括如下:初始化ws2_32.dll动态库-->创建套接字-->绑定地址信息-->服务器进行监听/客户端连接服务器-->数据交换-->关闭套接字对象. 服务器端: 1 #include <Winsock2.h> 2 #include <Ws2tcpip.h> 3 #include <iostream> 4 5 #prag

winsock教程- windows下的socket编程(c语言实现)

winsock教程- windows下的socket编程(c语言实现) 使用winsock进行socket 编程 这是一个学习windows下socket编程(c语言)的快速指南.这是因为一下代码片段只能运行在windows下.windows API中的socket编程部分叫做winsock. 你电脑上做出的任何网络通信背后基本上都有socket,它是一个网络的基本组成部分.举个例子说当你在浏览器键入www.google.com的时候,socket连接到google.com并且取回那个页面然后才

[email&#160;protected],该符号在函数 _main 中被引用

原文:[email protected],该符号在函数 _main 中被引用 int WSAStartup( __in WORD wVersionRequested, __out LPWSADATA lpWSAData ); WSAStartup 格  式: int PASCAL FAR WSAStartup( WORD wVersionRequested,  LPWSADATA lpWSAData ); 参  数:  wVersionRequested 欲使用的 Windows Sockets

在windows下用C语言写socket通讯实例

原文:在windows下用C语言写socket通讯实例 From:Microsoft Dev Center #undef UNICODE #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> #include <stdlib.h> #include <stdio.h> // Need to link with Ws2

编程实现与时间戳服务器的通信

在上一篇博文<介绍一个法国的时间戳服务器>中,介绍了获取时间戳响应的方法,分为两步: 1. 使用 OpenSSL 生成一个时间戳请求文件: 2. 使用 Linux 下的 curl 命令,发送时间戳请求.接收时间戳响应. 要想在 Windows 下执行第 2 步,有两种途径: 1) 下载为 Windows 平台实现的 curl 程序,可以到 http://curl.haxx.se/download.html 下载: 2)不用 curl 命令,自己编程实现向那个法国的时间戳服务器发送时间戳请求.接

Delphi-IP地址的隐藏

IP地址的隐藏 一.前言 本文主要介绍如何在程序中实现IP地址的隐藏.其实这篇东西不算我写的.其中<IP头结构>部分我懒得打字,故复制.粘贴了孤独剑客的文章,先说声谢谢!代码部分参考了外国程序xes写的一个程序.所以这只是学习过程中的一个副产品.既然程序已经做好了,就顺便放上来跟大家一起交流,共同提高吧.本文只不过想说明一下IP数据的结构和发送机制.如果有人把它改为恶意IP攻击工具,后果自负. 二.IP头结构 我们知道,TCP/IP网络数据全部是通过封装在IP数据包中在Internet网上传送

网络编程socket套接字及其使用(六)

今天继续深入学习socket套接字的使用,上一篇文章我们主要深入介绍了如何使用套接字进行面向连接的通信编程,这里我们主要深入讲解一下基于面向非连接的编程. 对于TCP面向连接来说,在通信前,需要经过三步握手协议以建立连接,而建立连接的过程往往比较耗费时间.连接建立后,在聊天过程中,可能经过好吃一段时间,聊天的双方才会说一句话(网速比较慢的情况比较常见),那么连接是一个保存还是应先断开,等对方说话时再建立连接呢?也就是说,TCP协议的面向连接.数据确认和重传机制将会影响聊天的效率.所以我们就有必要