Winsock conflicts with winsock2

Source: Internet
Author: User
When I was a beginner in Windows Network Programming, I encountered two types of compilation Errors (102 Errors are displayed in the Build window of VC6), which are some of the undefined types or repeated definitions, I am very depressed. The first error message in both cases is: Error Case 1: mswsock. h (69): errorC2065: SOCKET: undecia

When I was a beginner in Windows Network Programming, I encountered two types of compilation Errors (102 Errors are displayed in the Build window of VC6), which are some of the undefined types or repeated definitions, I am very depressed. The first error message is: error 1: mswsock. h (69): error C2065: 'socket': undecia.

When I was a beginner in Windows Network Programming, I encountered two types of compilation Errors (102 Errors are displayed in the Build window of VC6), which are some of the undefined types or repeated definitions, I am very depressed. The first error message is:

Error 1: mswsock. h (69): error C2065: 'socket ': undeclared identifier

Error Scenario 2: winsock2.h (99): error C2011: 'fd _ set': 'struct 'type redefinition

Later, I calmed down and analyzed the error prompt and related documents carefully, and finally found the cause.

We know that Windows network programming requires at least two header files: winsock2.h and windows. h. An old version of winsock. h exists before WinSock2.0. The order in which these three headers are included has led to the above problems.

Let's take a look at the content of winsock2.h, which has the following macro definitions at the beginning of the file:

# Ifndef _ WINSOCK2API _

# Define _ WINSOCK2API _

# Define _ WINSOCKAPI _/* Prevent plugin sion of winsock. h in windows. h */

_ WINSOCK2API _ is easy to understand. This is the most common protection measure to prevent repeated inclusion of header files. _ WINSOCKAPI _ is defined to block the old file winsock. if the user first contains winsock2.h, it is not allowed to include winsock. h. Otherwise, the type is repeatedly defined. How is this done? It's easy because the winsock. h header also has the following protection measures:

# Ifndef _ WINSOCKAPI _

# Define _ WINSOCKAPI _

Let's look back at winsock2.h, which is followed by the following macro commands:

/*

* Pull in WINDOWS. H if necessary

*/

# Ifndef _ INC_WINDOWS

# Include

# Endif/* _ INC_WINDOWS */

The function is to automatically include windows. h (_ INC_WINDOWS defined in windows. h) to define the types and constants required by WinSock2.0.

Switch to windows. h and find winsock. We are surprised to find the following content:

# Ifndef WIN32_LEAN_AND_MEAN

# Include

# Include

# Include

# Include

# Ifndef _ MAC

# Include

# Include

# Include

# Include

# Endif

# Include

# Ifndef _ MAC

# Include

# If (_ WIN32_WINNT> = 0x0400)

# Include

# Include

# Else

# Include

# Endif/* _ WIN32_WINNT> = 0x0400 */

# Endif

// Some content is omitted here

# Endif/* WIN32_LEAN_AND_MEAN */

See? Windows. h will reverse include winsock2.h or winsock. h! Mutual inclusion is the source of all evil!

The following describes how the problem occurs.

Error scenario 1: We first include winsock2.h in our project and then windows. h. If WIN32_LEAN_AND_MEAN is not defined and _ WIN32_WINNT is greater than or equal to 0x400, then windows. h will be automatically introduced at the beginning of winsock2.h, while windows. h will automatically introduce mswsock. h. At this time, mswsock. the socket type used in h is not yet defined, so there will be an undefined type error.

Error 2: windows is included first. h contains winsock2.h. If WIN32_LEAN_AND_MEAN is undefined and _ WIN32_WINNT is undefined or its version is smaller than 0x400, then windows. h will automatically import the old winsock. h. This will cause redefinition when winsock2.h is included.

It should be noted that the function of the macro WIN32_LEAN_AND_MEAN is to reduce the size of the win32 header file to accelerate compilation, which is generally automatically defined by AppWizard in stdafx. h. _ WIN32_WINNT is used to enable special functions in a later operating system. For example, to use a WaitableTimer, the value of _ WIN32_WINNT must be greater than or equal to 0x400. Therefore, if you have not encountered the above two problems, it is likely that you have not conducted network programming under these conditions.

The problem is not over yet. In addition to the windows library files that come with VC, the Platform SDK of MS also contains these header files. We can see that the program that can be compiled well before has changed the path of the windows header file. The reason is very simple. The windows. h file in Platform SDK is different from the file in VC. The code in the same location is as follows:

# Ifndef WIN32_LEAN_AND_MEAN

# Include

# Include

# Include

# Include

# Ifndef _ MAC

# Include

# Include

# Include

# Include

# Endif

# Include

# Ifndef _ MAC

# Include

# Include // Here, winsock. h is included directly.

# Endif

# Ifndef NOCRYPT

# Include

# Include

# Include

# Endif

# Ifndef NOGDI

# Ifndef _ MAC

# Include

# Ifdef INC_OLE1

# Include

# Else

# Include

# Endif /*! INC_OLE1 */

# Endif /*! MAC */

# Include

# Endif /*! NOGDI */

# Endif/* WIN32_LEAN_AND_MEAN */

Alas, we can't help wondering why MS is engaged in so many tricks. what's even more annoying is that since the Code is different, windows. h, but there is no macro definition that can help the program identify whether the current file is included in VC or in PSDK.

Later, I wrote a header file to handle the inclusion problem of winsock2.h, named winsock2i. h. You only need to include this file first in the source file of WinSock2.0. However, due to the problems mentioned above, you need to manually define USING_WIN_PSDK when using PSDK. The source code is as follows:

//

// Winsock2i. h-Include winsock2.h safely.

//

// Copyleft 02/24/2005 by freefalcon

//

//

// When WIN32_LEAN_AND_MEAN is not defined and _ WIN32_WINNT is less than 0x400,

// If we include winsock2.h AFTER windows. h or winsock. h, we get some compiling

// Errors as following:

// Winsock2.h (99): error C2011: 'fd _ set': 'struct 'Type redefinition

//

// When WIN32_LEAN_AND_MEAN is not defined and _ WIN32_WINNT is not less than 0x400,

// If we include winsock2.h BEFORE windows. h, we get some other compiling errors:

// Mswsock. h (69): error C2065: 'socket ': undeclared identifier

//

// So, this file is used to help us to include winsock2.h safely, it shocould be

// Placed before any other header files.

//

# Ifndef _ WINSOCK2API _

// Prevent upload sion of winsock. h

# Ifdef _ WINSOCKAPI _

# Error Header winsock. h is wrongly ded unexpectedly.

# Endif

// NOTE: If you use Windows Platform SDK, you should enable following definition:

// # Define USING_WIN_PSDK

# If! Defined (WIN32_LEAN_AND_MEAN) & (_ WIN32_WINNT> = 0x0400 )&&! Defined (USING_WIN_PSDK)

# Include

# Else

# Include

# Endif

# Endif // _ WINSOCK2API _

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.