WinSock.h & WinSock2.h which to use?

2019-04-05 00:19发布

问题:

Does anyone know the differences between WinSock.h and WinSock2.h . I know they don't use the same library (.lib) but I don't know if WinSock2 only adds new features or if it also improves WinSock 1 features. I am working with IP/TCP sockets and want to manage timeouts with select(). I also use multiple threads to manage more than only one client at the same time. Should I continue using WinSock 1.1 or should I move to WinSock 2? Thanks in advance.

回答1:

You should probably use winsock2.h.

Few points:

  • winsock.h should be used with wsock32.lib and winsock2.h should be used with ws2_32.lib
  • winsock.h and winsock2.h should not be together in the same project, winsock2.h replaces winsock.h and not extends it.
  • winsock.h should only be used if you target old version of Windows like Windows 95 / Windows NT 3.5.

Microsoft implementations

Version 1.1 (winsock.h) of Winsock was supplied in an add-on package (called Wolverine) for Windows for Workgroups (code named Snowball). It was an integral component of Windows 95 and Windows NT from versions 3.5 and onwards (the initial commercially available version of Windows NT, version 3.1, included only a proprietary and quite incomplete implementation of TCP/IP based on the AT&T UNIX System V "Streams" API.

Version 2.1 (winsock2.h) of Winsock was supplied in an add-on package for Windows 95. It was an integral component of Windows 98, Windows NT 4.0, and all subsequent Windows releases. Recent versions of Winsock 2.x have been delivered with new Windows releases or as part of service packs.

Does Winsock2 adds features?

Winsock 2 is extensible by a mechanism known as a Layered Service Provider (LSP). Winsock LSPs are available for a wide range of useful purposes, including Internet parental controls, web content filtering, QoS etc.



回答2:

Winsock.h is there to keep code compiling that started life in the 16-bit version of Windows. You should always use Winsock2.h and link to ws2_32.lib in new projects.