Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Hi all,
I'm using VC++ 6.0 (and I don't want to change it becuase of my own reasons) and I'm programming a simple client-server using winsock2.h.

The problem is when I include winsock2.h and compile it the VC++ have 59 errors.
such these errors:
c:\program files\microsoft visual studio\vc98\include\winsock2.h(99) : error C2011: 'fd_set' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(134) : warning C4005: 'FD_SET' : macro redefinition
c:\program files\microsoft visual studio\vc98\include\winsock.h(83) : see previous definition of 'FD_SET'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(143) : error C2011: 'timeval' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(199) : error C2011: 'hostent' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(212) : error C2011: 'netent' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(219) : error C2011: 'servent' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(226) : error C2011: 'protoent' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(310) : error C2011: 'in_addr' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(368) : error C2011: 'sockaddr_in' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(378) : error C2011: 'WSAData' : 'struct' type redefinition
c:\program files\microsoft visual studio\vc98\include\winsock2.h(430) : warning C4005: 'SO_DONTLINGER' : macro redefinition
c:\program files\microsoft visual studio\vc98\include\winsock.h(391) : see previous definition of 'SO_DONTLINGER'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(483) : warning C4005: 'AF_IPX' : macro redefinition
c:\program files\microsoft visual studio\vc98\include\winsock.h(449) : see previous definition of 'AF_IPX'
c:\program files\microsoft visual studio\vc98\include\winsock2.h(506) : warning C4005: 'AF_MAX' : macro redefinition

Why this happens?? what the solution of my problem??
Posted

1 solution

If you look a little closer, you will notice the compiler is telling you to "see previous definition of xxxx", which is found in winsock.h. In other words, somewhere you are including winsock.h. Get rid of that include and that should fix these errors.

Hopefully that will not introduce a bunch of new errors :). If you do have problems after deleting include <winsock.h>, the way to fix it is to have the following in your stdafx.h:
C++
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>

Refer to this[^] MSDN article.
Note from MSDN article:
The Winsock2.h header file internally includes core elements from the Windows.h header file, so there is not usually an #include line for the Windows.h header file in Winsock applications. If an #include line is needed for the Windows.h header file, this should be preceded with the #define WIN32_LEAN_AND_MEAN macro. For historical reasons, the Windows.h header defaults to including the Winsock.h header file for Windows Sockets 1.1. The declarations in the Winsock.h header file will conflict with the declarations in the Winsock2.h header file required by Windows Sockets 2.0. The WIN32_LEAN_AND_MEAN macro prevents the Winsock.h from being included by the Windows.h header.

Soren Madsen
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900