对Winsock2.dll和Winsock2.h感到困惑?(Confused about Winsock2.dll vs Winsock2.h?)

编程入门 行业动态 更新时间:2024-10-25 22:26:25
对Winsock2.dll和Winsock2.h感到困惑?(Confused about Winsock2.dll vs Winsock2.h?)

我有点困惑,我正在尝试制作一个没有外部依赖的程序(因此它可以很容易地移植到UNIX),它具有套接字连接功能。 但我不确定我应该使用什么?

在Windows XP,7,8上是否有一个可以链接的标准库,可以输出一个可以单独运行的exe(不依赖于DLL来运行)? 例如,在UNIX上,我可以链接到socket并使用sys/socket.h并且有95%的UNIX没有做任何事情。

I am a little confused, I am trying to make a program with no external dependencies (so it could be easily ported to UNIX for example) that has socket connection abilities. But I am unsure of what I should be using?

Is there a standard library on Windows XP, 7, 8 that can be linked against that can output a single exe that can run alone (not depend on a DLL to function)? For example on UNIX I can link against socket and use sys/socket.h and have like 95% of UNIX's without doing anything.

最满意答案

相同的代码对两者都不起作用。 但是你可以使用#ifdef WIN32使预处理器只在Windows下编译时考虑Windows的东西。

在标题中:

#ifdef WIN32 // Windows exclusive dependencies go here #include <windows.h> #include <winsock.h> #pragma comment(lib, "ws2_32.lib") #define close(s) closesocket(s) #else // Linux exclusive dependencies go here #include <sys/socket.h> #endif

在main()你需要做这样的事情:

#ifdef WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); #endif

我相信这就是全部,API的其余部分与使用套接字的UNIX方式广泛兼容。

Same code just won't do for both. But you can use #ifdef WIN32 to make the preprocessor consider Windows stuff only when compiling under Windows.

In the headers:

#ifdef WIN32 // Windows exclusive dependencies go here #include <windows.h> #include <winsock.h> #pragma comment(lib, "ws2_32.lib") #define close(s) closesocket(s) #else // Linux exclusive dependencies go here #include <sys/socket.h> #endif

And inside main() you will need to do something like this:

#ifdef WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2, 2), &wsaData); #endif

I believe this is all, the rest of the API is widely compatible with the UNIX way of using sockets.

更多推荐

本文发布于:2023-08-05 00:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1424619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:困惑   dll   Confused

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!