C语言 在 windows 上使用 sys/socket.h 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2952733/
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
Using sys/socket.h functions on windows
提问by BSchlinker
I'm attempting to utilize the socket.h functions within Windows. Essentially, I'm currently looking at the sample code at https://beej.us/guide/bgnet/html/multi/clientserver.html#datagram. I understand that socket.h is a Unix function -- is there anyway I can easily emulate that environment while compiling this sample code? Does a different IDE / compiler change anything?
我正在尝试在 Windows 中使用 socket.h 函数。本质上,我目前正在查看https://beej.us/guide/bgnet/html/multi/clientserver.html#datagram上的示例代码。我知道 socket.h 是一个 Unix 函数——无论如何我可以在编译此示例代码时轻松模拟该环境吗?不同的 IDE/编译器会改变什么吗?
Otherwise, I imagine that I need to utilize a virtualized Linux environment, which may be best anyways as the code will most likely be running in a UNIX environment.
否则,我想我需要利用虚拟化的 Linux 环境,这可能是最好的,因为代码很可能在 UNIX 环境中运行。
Thanks.
谢谢。
回答by leonbloy
You have two options:
您有两个选择:
Cygwin: lets you compile your Unix sources mostly untouched, but ties you to the Cygwin emulation library. This have two implications: general performance -no only network- will probably be less than optimal; and the target environment must have (at run time) the Cygwin DLL installed.
Cygwin:让你编译你的 Unix 源代码,大部分是原封不动的,但将你与 Cygwin 仿真库联系起来。这有两个含义:一般性能——不仅仅是网络——可能不是最佳的;并且目标环境必须(在运行时)安装了 Cygwin DLL。
Winsock: this requires you to replace sys/socket.h(BSD sockets library, UNIX standard for the TCP/IP stack) with winsock2.h, and rewrite some parts of the code - not much, but some.
Winsock:这需要您将sys/socket.h(BSD 套接字库,TCP/IP 堆栈的 UNIX 标准)替换为winsock2.h,并重写代码的某些部分 - 不多,但有一些。
Some related questions with valuable info:
一些具有有价值信息的相关问题:
Differences between winsock and BSD socket implementations
Some Issues About Cygwin[Linux in Windows] (socket,thread,other programming and shell issues)
回答by INS
Writing cross platform network applications is not easy with what the BSD standard provides you. Sure it will work but you'll have to make some replacements like replacing ioctl (if needed) with ioctlsocket (on windows). More differences here.
使用 BSD 标准为您提供的内容来编写跨平台网络应用程序并不容易。当然它会起作用,但您必须进行一些替换,例如用 ioctlsocket(在 Windows 上)替换 ioctl(如果需要)。更多差异在这里。
My advice is to use a library that hides these ugly differences and provides a unified way of communicating. I personally use ACE. You have plenty of examples that show you how to create a server and a client. Copy from the samples and see how they do it there. Their mailing lists are of great help also (don't forget to use the PRF - see the source tree for the Problem-Report-Form). You can borrow the books for more information. Important note: by using ace you can use ACE wrapper functions like socket, setsockopt, ioctl etc. without worry that they will not work. Unfortunately this adds a library dependency which is not always an option. But ACE has more powerfull features which I'm sure you'll like once you discover them. Hints: Reactor, Proactor.
我的建议是使用一个可以隐藏这些丑陋差异并提供统一通信方式的库。我个人使用ACE。您有很多示例可以向您展示如何创建服务器和客户端。从样本中复制,看看他们是如何做到的。他们的邮件列表也很有帮助(不要忘记使用 PRF - 请参阅问题报告表的源代码树)。您可以借书了解更多信息。重要提示:通过使用 ace,您可以使用 ACE 包装函数,如 socket、setsockopt、ioctl 等,而不必担心它们将无法工作。不幸的是,这增加了一个库依赖项,这并不总是一个选项。但是 ACE 具有更强大的功能,我相信一旦您发现它们,您会喜欢的。提示:反应器,前摄器。

