Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
634 views
in Technique[技术] by (71.8m points)

c++ - Is there a difference between <winsock.h> and <winsock2.h>?

I'm including as it's required by the MySQL C library.

The auto-complete in VS2010 is also showing a - any idea what this is?

Are they interchangeable, and are there any advantages of one over the other?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

@cost's answer links to a discussion that, amongst other things, asks this question that was never answered:

Is there a reason why I can't include windows.h before winsock2.h, it gives me tons of errors, but once I switch their order everything is okay... why is that?

windows.h includes winsock2.h when compiling for newer Windows versions, but for older development it includes winsock.h instead. The problem is not limited to just windows.h, though. Any time winsock.h gets included before winsock2.h, there will be compiler errors. The reason is because the two files DO NOT co-exist very well. winsock2.h was designed to replace winsock.h, not extend it. Everything that is defined in winsock.h is also defined in winsock2.h. If winsock2.h is included before winsock.h, winsock2.h defines _WINSOCKAPI_ to prevent the compiler from processing subsequent winsock.h includes, and all is fine. But if winsock.h is included before winsock2.h, winsock2.h does not detect that and tries to re-define everything that winsock.h has already defined, causing the compile to fail.

You have to be very careful when mixing code that uses winsock.h with code that uses winsock2.h in the same project. For instance, when writing your own socket code that uses winsock2.h, and using third-party libraries that still use winsock.h.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...