< winsock.h>和< winsock2.h>? [英] Is there a difference between <winsock.h> and <winsock2.h>?

查看:187
本文介绍了< winsock.h>和< winsock2.h>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



VS2010中的自动完成功能也显示了 - 任何想法是什么?

p>

它们是否可以互换,并且一方比另一方有什么优点?



谢谢!

解决方案

@ cost's answer 链接,其中包含了此问题未回答的讨论:


有一个原因,我不能包括windows.h前winsock2.h,
它给我很多错误,但一旦我切换他们的订单一切
是好吧为什么是<?p

windows.h 包括 winsock2 .h ,但对于较旧的开发,它包括 winsock.h 。但问题不仅限于 windows.h 。任何时候 winsock.h 包含在 winsock2.h 之前,都会有编译器错误。原因是因为两个文件 DO NOT 并存非常好。 winsock2.h 旨在替换 winsock.h ,不扩展它。在 winsock.h 中定义的所有内容也在 winsock2.h 中定义。如果 winsock.h 之前包含winsock2.h $ c>定义 _WINSOCKAPI _ 以防止编译器处理后续的 winsock.h 包含,一切都很好。但如果 winsock2.h 之前包含 winsock.h winsock2.h 没有检测到并尝试重新定义 winsock.h 已经定义的所有内容,导致编译失败。



混合使用 winsock.h 的代码时,必须非常小心,使用 winsock2.h 在同一个项目中。例如,当编写使用 winsock2.h 的自己的套接字代码并使用仍然使用 winsock.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!

解决方案

@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.

这篇关于&lt; winsock.h&gt;和&lt; winsock2.h&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆