C++ 从哪里获得“sys/socket.h”头文件/源文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4638604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:00:51  来源:igfitidea点击:

Where does one get the "sys/socket.h" header/source file?

c++socketsunix

提问by AnonymousJohn

I've been trying to write a server in C++ Unix style, but I'm stuck on a Windows machine. I started with MinGW, but it didn't compile right and told me that it couldn't find the "sys/socket.h" file. Which, of course, is necessary for the server to even work. I went searching for it, and I think somewhere said to install Cygwin instead, as it comes with lots of libraries. Okay, so I installed it. Every single library it could possibly offer me. Then I went to compile again, and it STILL can't find it. I went searching through the ENTIRE includes folder, and couldn't find the file. So I was a little miffed (3 hours down the drain for extra functionality I don't need), but I continued to search. I can't find it ANYWHERE. I find multiple references to using it, but I can't find ANYWHERE to download it. I've been searching for the past few hours now, and I've become very frustrated with everything because there are NO references to where I can get it (and I will NOT use winsock. That breaks compatibility if I remember right).

我一直在尝试用 C++ Unix 风格编写一个服务器,但我被困在一台 Windows 机器上。我从 MinGW 开始,但它没有正确编译并告诉我它找不到“sys/socket.h”文件。这当然是服务器正常工作所必需的。我去寻找它,我想某处说要安装 Cygwin,因为它带有很多库。好的,所以我安装了它。它可能为我提供的每一个图书馆。然后我又去编译了,还是找不到。我搜索了整个包含文件夹,但找不到该文件。所以我有点恼火(为了我不需要的额外功能而浪费了 3 个小时),但我继续搜索。我在任何地方都找不到。我找到了多个使用它的参考,但我找不到任何地方来下载它。

So, long story short, where can I download the 'socket.h'/'socket.c'/'socket.cpp' files? It would make my life (and I'm sure many others' lives) SO much easier, and I would really appreciate it!

所以,长话短说,我在哪里可以下载“socket.h”/“socket.c”/“socket.cpp”文件?它会让我的生活(我相信很多其他人的生活)变得更轻松,我真的很感激!

回答by Jeremy Friesner

Given that Windows has no sys/socket.h, you might consider just doing something like this:

鉴于 Windows 没有 sys/socket.h,你可能会考虑做这样的事情:

#ifdef __WIN32__
# include <winsock2.h>
#else
# include <sys/socket.h>
#endif

I know you indicated that you won't use WinSock, but since WinSock is how TCP networking is done under Windows, I don't see that you have any alternative. Even if you use a cross-platform networking library, that library will be calling WinSock internally. Most of the standard BSD sockets API calls are implemented in WinSock, so with a bit of futzing around, you can make the same sockets-based program compile under both Windows and other OS's. Just don't forget to do a

我知道您表示您不会使用 WinSock,但由于 WinSock 是在 Windows 下完成 TCP 网络的方式,我认为您没有其他选择。即使您使用跨平台网络库,该库也会在内部调用 WinSock。大多数标准的 BSD 套接字 API 调用是在 WinSock 中实现的,因此,稍加考虑,您就可以在 Windows 和其他操作系统下编译相同的基于套接字的程序。只是不要忘记做一个

#ifdef __WIN32__
   WORD versionWanted = MAKEWORD(1, 1);
   WSADATA wsaData;
   WSAStartup(versionWanted, &wsaData);
#endif

at the top of main()... otherwise all of your socket calls will fail under Windows, because the WSA subsystem wasn't initialized for your process.

在 main() 的顶部...否则您的所有套接字调用都将在 Windows 下失败,因为 WSA 子系统没有为您的进程初始化。

回答by Michael Aaron Safyan

Since you've labeled the question C++, you might be interested in using boost::asio, ACE, or some other cross-platform socket library for C++ or for C. Some other cross-platform libraries may be found in the answers to C++ sockets library for cross-platformand Cross platform Networking API.

既然你已经标记的问题C ++,你可能有兴趣使用的boost :: ASIOACE,或其他一些跨平台的套接字库用于C ++或C.其他一些跨平台的库可以在答案中找到C ++用于跨平台跨平台网络 API 的套接字库

Assuming that using a third party cross-platform sockets library is not an option for you...

假设使用第三方跨平台套接字库不适合您......

The header <sys/socket.h>is defined in IEEE Std. 1003.1 (POSIX), but sadly Windows is non-compliant with the POSIX standard. The MinGW compiler is a port of GCC for compiling Windows applications, and therefore does not include these POSIX system headers. If you install GCC using Cygwin, then it will include these system headers to emulate a POSIX environment on Windows. Be aware, however, that if you use Cygwin for sockets that a.) you will need to put the cygwin DLL in a place where your application can read it and b.) Cygwin headers and Windows headers don't interact very well (so if you plan on including windows.h, then you probably don't want to be including sys/socket.h).

头文件<sys/socket.h>在 IEEE Std 中定义。1003.1 (POSIX),但遗憾的是 Windows 不符合 POSIX 标准。MinGW 编译器是 GCC 的一个端口,用于编译 Windows 应用程序,因此不包括这些 POSIX 系统头文件。如果您使用Cygwin安装 GCC ,那么它将包含这些系统头文件以在 Windows 上模拟 POSIX 环境。但是请注意,如果您将 Cygwin 用于套接字 a.) 您需要将 cygwin DLL 放在您的应用程序可以读取它的地方 b.) Cygwin 头文件和 Windows 头文件不能很好地交互(所以如果您打算包含windows.h,那么您可能不想包含sys/socket.h)。

My personal recommendation would be to download a copy of VirtualBox, download a copy of Ubuntu, install Ubuntu into VirtualBox, and then do your coding and testing on Ubuntu. Alternatively, I am told that Microsoft sells a "UNIX subsystem" and that it is pre-installed on certain higher-end editions of Windows, although I have no idea how compliant this system is (and, if it is compliant, with which edition of the UNIX standard it is compliant). Winsockets are also an option, although they can behave in subtly different ways than their POSIX counterparts, even though the signatures may be similar.

我个人的建议是下载VirtualBox的副本,下载 Ubuntu 的副本,将 Ubuntu安装到 VirtualBox 中,然后在 Ubuntu 上进行编码和测试。或者,我被告知 Microsoft 销售“UNIX 子系统”并且它预装在某些更高端的 Windows 版本上,尽管我不知道这个系统的兼容性如何(如果它符合,它是哪个版本它符合 UNIX 标准)。Winsockets 也是一种选择,尽管它们的行为方式与它们的 POSIX 对应物略有不同,即使签名可能相似。

回答by Adamos19

I would like just to add that if you want to use windows socket library you have to :

我想补充一点,如果你想使用 Windows 套接字库,你必须:

  • at the beginning : call WSAStartup()

  • at the end : call WSACleanup()

  • 开始时:调用 WSAStartup()

  • 最后:调用 WSACleanup()

Regards;

问候;

回答by Adamos19

Try to reinstall cygwin with selected package:gcc-g++ : gnu compiler collection c++ (from devel category), openssh server and client program (net), make: the gnu version (devel), ncurses terminal (utils), enhanced vim editors (editors), an ANSI common lisp implementation (math) and libncurses-devel (lib).

尝试使用选定的包重新安装 cygwin:gcc-g++:gnu 编译器集合 c++(来自 devel 类别)、openssh 服务器和客户端程序(net)、make:gnu 版本(devel)、ncurses 终端(utils)、增强的 vim 编辑器(编辑器)、ANSI 通用 lisp 实现(数学)和 libncurses-devel(lib)。

This library files should be under cygwin\usr\include

这个库文件应该在 cygwin\usr\include 下

Regards.

问候。