将 Winsock 移植到 Linux 套接字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4617478/
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
Porting Winsock to Linux Sockets
提问by Mike Bailey
I have a program that does some networking using Winsock, and one of our requirements right now is to port over our program to Linux. The only thing stopping us from doing this is Winsock.
我有一个使用 Winsock 进行网络连接的程序,我们现在的要求之一是将我们的程序移植到 Linux。唯一阻止我们这样做的是 Winsock。
My question is: How easy can I port this over to a Linux implementation?
我的问题是:将它移植到 Linux 实现上有多容易?
Are there any pitfalls I should be aware of, and if I simply include the appropriate header files, what sort of things will I have to be sure to handle?
是否有任何我应该注意的陷阱,如果我只是包含适当的头文件,我必须确保处理哪些类型的事情?
Thanks for any help!
谢谢你的帮助!
I'd post code but I can't unfortunately due to legal reasons. But, our code does use the following:
我会发布代码,但不幸的是由于法律原因我不能发布。但是,我们的代码确实使用了以下内容:
WSAStartup(..)
WSACleanup(..)
Socket(..)
sendto(..)
recvfrom(..)
ioctlsocket(..)
setsocketopt(..)
采纳答案by Len Holgate
It will depend if you use any windows specific networking functionality or if you're just using mostly the mostly BSD compatible API.
这将取决于您是否使用任何 Windows 特定的网络功能,或者您是否只使用大多数与 BSD 兼容的 API。
So, if you're using overlapped I/O and I/O completion ports and other advanced parts of the Winsock API then things will be very difficult to port and if you're just using the BSD compatible stuff then it should be easy to write a thin translation layer or even just have the winsock startup and shutdown stuff inside a windows specific ifdef...
所以,如果你使用重叠的 I/O 和 I/O 完成端口以及 Winsock API 的其他高级部分,那么事情将非常难以移植,如果你只是使用 BSD 兼容的东西,那么应该很容易编写一个薄的翻译层,或者甚至只是在特定于 Windows 的 ifdef 中包含 winsock 启动和关闭内容...
This may help: http://tangentsoft.net/wskfaq/articles/bsd-compatibility.html
这可能会有所帮助:http: //tangentsoft.net/wskfaq/articles/bsd-compatibility.html
回答by hometoast
Without seeing code, it's tough to say how easy it is. But you shouldbe able to replace winsock calls to analogs in sys/sockets.h.
没有看到代码,很难说它有多容易。但是您应该能够将 winsock 调用替换为 sys/sockets.h 中的模拟。
回答by Joshua
The only calls that make porting difficult are the WSA* calls.
唯一使移植变得困难的调用是 WSA* 调用。
WSAStartup() -> nop WSACleanup() -> nop
WSAStartup() -> nop WSACleanup() -> nop
Socket/setsockopt -> socket/setsockopt
套接字/setsockopt -> 套接字/setsockopt
Under *nix, sockets are blocking by default and it's not necessary or possible to use that weird setsockopt call to fiddle with it.
在 *nix 下,默认情况下套接字是阻塞的,并且没有必要或不可能使用那个奇怪的 setockopt 调用来摆弄它。
ioctlsocket -> ioctl
ioctlsocket -> ioctl
Under *nix we don't like asynchronous sockets much and prefer to use the select() system call.
在 *nix 下,我们不太喜欢异步套接字,而更喜欢使用 select() 系统调用。
---- Rest of this answer seems only to apply to Win95 compatible winsock ----
---- 这个答案的其余部分似乎只适用于兼容 Win95 的 winsock ----
Unfortunately as the original socket() in Winsock was broken in some cases, you probably used WSASocket() and so have to convert those calls.
不幸的是,由于 Winsock 中的原始 socket() 在某些情况下被破坏,您可能使用了 WSASocket(),因此必须转换这些调用。
回答by Ben Voigt
Based on that list of functions, things should more or less just work. Add #if _WIN32
around the calls to WSAStartup
and WSACleanup
(the linux equivalent is to not do anything, the sockets library is initialized automatically).
根据该功能列表,事情应该或多或少可以正常工作。添加#if _WIN32
对WSAStartup
and的调用WSACleanup
(Linux 等价物是不做任何事情,sockets 库是自动初始化的)。
You also might need some OS-dependent code when setting socket options, some of them are the same, some aren't, and the types might be different.
在设置套接字选项时,您可能还需要一些与操作系统相关的代码,其中一些是相同的,一些不是,并且类型可能不同。