本文介绍了从哪里获得“sys/socket.h"?头文件/源文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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!

推荐答案

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

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

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

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

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

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

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

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.

这篇关于从哪里获得“sys/socket.h"?头文件/源文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 07:25