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

WSAStartup function的更多相关文章

  1. WSAStartup()函数的使用

    int WSAStartup( __in WORD wVersionRequested, __out LPWSADATA lpWSAData ); WSAStartup 格  式: int PASCA ...

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

    winsock教程- windows下的socket编程(c语言实现) 使用winsock进行socket 编程     这是一个学习windows下socket编程(c语言)的快速指南.这是因为一下 ...

  3. _WSAStartup@8,该符号在函数 _main 中被引用

    int WSAStartup( __in WORD wVersionRequested, __out LPWSADATA lpWSAData ); WSAStartup 格  式: int PASCA ...

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

    原文:在windows下用C语言写socket通讯实例 From:Microsoft Dev Center #undef UNICODE #define WIN32_LEAN_AND_MEAN #in ...

  5. windows socket函数详解

    windows socket函数详解 近期一直用第三方库写网络编程,反倒是遗忘了网络编程最底层的知识.因而产生了整理Winsock函数库的想法.以下知识点均来源于MSDN,本人只做翻译工作.虽然很多前 ...

  6. Delphi-IP地址的隐藏

    IP地址的隐藏 一.前言 本文主要介绍如何在程序中实现IP地址的隐藏.其实这篇东西不算我写的.其中<IP头结构>部分我懒得打字,故复制.粘贴了孤独剑客的文章,先说声谢谢!代码部分参考了外国 ...

  7. [JSBSim]基于winsocket2的TCP\UDP使用例子

    TCP部分: 参考:http://blog.csdn.net/sbfksmq/article/details/50808863 另附:linux下的tcp/udp参考:https://www.cnbl ...

  8. c++下基于windows socket的单线程服务器客户端程序(基于TCP协议)

    今天自己编写了一个简单的c++服务器客户端程序,注释较详细,在此做个笔记. windows下socket编程的主要流程可概括如下:初始化ws2_32.dll动态库-->创建套接字-->绑定 ...

  9. c++下基于windows socket的服务器客户端程序(基于UDP协议)

    前天写了一个基于tcp协议的服务器客户端程序,今天写了一个基于UDP协议的,由于在上一篇使用TCP协议的服务器中注释已经较为详细,且许多api的调用是相同的,故不再另外注释. 使用UDP协议需要注意几 ...

随机推荐

  1. 剑指offer+名企面试官精讲典型编程题,28题扩展题

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  2. T4 模板代码生成

    <# //********************************************************* // // Copyright (c) Microsoft. All ...

  3. junit里面Test Case的执行顺序

    这里讨论的是junit在ant运行的情况,其他build工具应该也适用,但具体没试验过. 首先运行junit时是按照脚本中文件夹的顺序执行,如下脚本会先执行test1目录下的测试,其实是test2目录 ...

  4. java-IO-基本输出输入流

    / 标准输入输出流: 直接类名调用 一经创建无法改变 public static final PrintStream err “标准”错误输出流. public static final InputS ...

  5. Linux 环境下 javac 编译错误: 编码UTF8的不可映射字符 (编码UTF8/GBK的不可映射字符)

    Linux 系统下一般默认使用UTF-8编码, 使用javac 编辑使用其他编码格式编写的源吗时,会出现  “ 错误: 编码UTF8的不可映射字符 ”. 最近在使用  javac 编译 一个在wind ...

  6. test20181018 B君的第一题

    题意 分析 考场爆零做法 考虑dp,用\(f(i,j,0/1)\)表示i及其子树中形成j个边连通块的方案数,其中i是否向外连边. \(O(n^3)\),转移方程太复杂就打挂了. #include< ...

  7. MySQL的WHERE语句中BETWEEN与IN的用法和他们的区别

    MySQL BETWEEN 用法 not可以对between...and取反. 1.数值型 BETWEEN 运算符用于 WHERE 表达式中,选取介于两个值之间的数据范围.BETWEEN 同 AND ...

  8. pthread中取消线程

    取消线程:告诉一个线程关掉自己,取消操作允许线程请求终止其所在进程中的任何其他线程.不希望或不需要对一组相关的线程执行进一步操作时,可以选择执行取消操作.取消线程的一个示例是异步生成取消条件. 对于c ...

  9. oracle12c之 表空间维护总结

    1.1.创建永久表空间 In the CDB:SQL> CONNECT system@cdb1SQL> CREATE TABLESPACE cdb_users DATAFILE'/home ...

  10. 希尔排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def ShellSort(a): gap = a.size / 2 while gap >= 1: for ...