MinGW 中的套接字

发布于 2024-08-06 09:53:40 字数 923 浏览 7 评论 0原文

我只是尝试使用 MinGW 在 MSYS 中构建 netcat,并意识到 MinGW 从未真正将所有 BSD 套接字内容移植到 Windows(例如 sys/socket.h)。我知道你可以在 MinGW 中使用 Windows 套接字,但为什么他们从来没有制作 BSD 套接字的 Windows 端口?我注意到很多程序使用#ifdef 来解决这个问题。是否有 BSD 套接字的 Windows 端口可以替代使用?

以下是在 MSYS 中为 netcat 执行 make 时出现的错误:


gcc -DLOCALEDIR=\"\/usr/local/share/locale\" -DHAVE_CONFIG_H -I. -I. -I.. -g -O2 -Wall -c `test -f 'core.c' || echo './'`core.c
In file included from core.c:29:
netcat.h:38:24: sys/socket.h: No such file or directory
netcat.h:39:63: sys/uio.h: No such file or directory
netcat.h:41:24: netinet/in.h: No such file or directory
netcat.h:42:55: arpa/inet.h: No such file or directory

MinGW 没有 #ifdef。是否有一个库/包我可以添加到 MSYS 以使所有内容编译时没有错误?

注意:您可以在此处下载 netcat 并浏览 CVS 存储库此处

I was just trying to build netcat in MSYS using MinGW and realized that MinGW never really ported all of the BSD socket stuff to Windows (eg sys/socket.h). I know you can use Windows Sockets in MinGW, but why did they never make a Windows port of the BSD sockets? I noticed quite a few programs using #ifdef's to workaround the issue. Is there a Windows port of the BSD sockets somewhere that can be used instead?

Here are the errors when doing a make for netcat in MSYS:


gcc -DLOCALEDIR=\"\/usr/local/share/locale\" -DHAVE_CONFIG_H -I. -I. -I.. -g -O2 -Wall -c `test -f 'core.c' || echo './'`core.c
In file included from core.c:29:
netcat.h:38:24: sys/socket.h: No such file or directory
netcat.h:39:63: sys/uio.h: No such file or directory
netcat.h:41:24: netinet/in.h: No such file or directory
netcat.h:42:55: arpa/inet.h: No such file or directory

There are no #ifdef's for MinGW. Is there a library/package I can add to MSYS to make everything compile without errors?

Note: You can download netcat here and browse the CVS repo here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

软糯酥胸 2024-08-13 09:53:40

BSD sys/socket.h 是一个 POSIX 标头, win32 API 不支持它。 MinGW 标头只是本机 win32 标头的重新实现,不提供额外的 POSIX 兼容性。

如果您正在寻找 sys/socket.h 支持,请尝试 GNU gnulib 的 sys /socket.h 替换 或使用 Cygwin,它在 Windows 上提供 POSIX 兼容性包装器。

BSD sys/socket.h is a POSIX header and the win32 API doesn't support it. MinGW headers are just a reimplementation of native win32 headers and don't offer additional POSIX compatibility.

If you are looking for sys/socket.h support, try either GNU gnulib's sys/socket.h replacement or go with Cygwin, which provides a POSIX compatibility wrapper on Windows.

左耳近心 2024-08-13 09:53:40

WinSock 和 WinSock2 具有与 BSD 套接字不同的函数名称。如果我想编写跨平台应用程序,那么我会编写很多变通办法,只是为了让微软满意。

如果 MinGW 中包含特殊的“socket.h”和“socket.c”文件,通过调用相应的 WinSock2 对应部分来简单地翻译内容,那就容易多了。

我刚刚开始学习 C 编程,所以我自己无法做到这一点,但令我惊讶的是,到目前为止似乎没有人尝试过这样做。

WinSock and WinSock2 have different function names from the BSD Sockets. If I wish to write cross-platform applications, then I have code a lot of work-arounds just to keep Microsoft happy.

It would be so much easier if there were special "socket.h" and "socket.c" files included with MinGW that simply translated stuff by calling the respective WinSock2 counter-parts.

I'm just starting to learn C programming, so I'm unable to do this myself, but I'm surprised that nobody seems to have even attempted this so far.

沙沙粒小 2024-08-13 09:53:40

来自另一个答案的这些评论充当了我需要获得一段简单的 bsd 套接字代码以在 Windows 上使用 mingw 进行编译的答案。

将所有这些包含内容替换为#include,这样就可以了
是winsock 的等效标头,然后看看会发生什么。

您还需要链接到 ws2_32 并使用
WSAStartup/WSACleanup。这可能会让您启动并运行。

编辑:
我最终还不得不将 close 替换为 shutdown / closesocket 并将 write 替换为 send >。
代码编译得很好,但如果没有这些额外的更改,实际上就无法工作。

These comments from another answer served as the answer I needed to get a piece of simple bsd socket code to compile with mingw on windows.

Replace all of those includes with #include as that would
be the equivalent header for winsock, then see what happens.

You will also need to link against ws2_32 and use
WSAStartup/WSACleanup. Which might get you up and running.

EDIT:
I also ended up having to replace close with shutdown / closesocket and write with send.
The code compiled fine but didn't actually work without those additional changes.

酒几许 2024-08-13 09:53:40

正如 ChrisW 所说,Winsock2 是 BSD 套接字的一个端口。您尝试使用的winsock 的哪一部分与BSD 套接字不同? (WSAStartup 和 WSACleanup 除外)

As ChrisW said, Winsock2 is a port of BSD sockets. Which part of winsock are you trying to use which differs from BSD sockets ? (other than the WSAStartup and WSACleanup)

慈悲佛祖 2024-08-13 09:53:40

MingWin 是极简主义的,这是它最重要的方面。因为它更容易理解,所以最终开发人员有责任编写应用程序。 MingWin 只会让事情变得更容易,但在将 nix 应用程序转向 Windows 方面并没有什么神奇之处。

MingWin is minimalist, and that is the most important aspect of it. Because it makes it easier to understand, at the end it is the developer's responsibility to write the application. MingWin only makes things easier but does no magic in turing nix apps to windows.

冰雪梦之恋 2024-08-13 09:53:40

请参阅 stackoverflow 链接:从哪里获得“sys/socket.h”头文件/源文件?

答案/解决方案更明确。

See the stackoverflow link : Where does one get the "sys/socket.h" header/source file?

The answer/solution is more explicit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文