winsock不支持读/写
通过一个小型测试程序(在 Linux 上用 mingw 编译),我注意到无法在套接字 fd 上使用 read
和 write
调用,这是使用 Winsock2 的 实现获得的。代码>套接字调用。写入调用返回 <0 并设置 errno=EBADF。
想象一下从 xinetd 运行的程序,减去它们的 stdin/stdout 始终是套接字的假设。 (例如,某些程序确实会调用 getpeername,如果它不是套接字,则会失败,随后它们可能会过早退出。)
因此,仅从 stdin/stdout 读取/写入的与 {文件描述符类型} 无关的程序应该如何除非对 fd 做出假设,否则可以在 win32 环境中合理工作吗?
或者更简单地说,是否需要执行一些神奇的函数调用来将 Winsock2 套接字 fds 与 win32(好吧,mingw)write
实现连接起来?
With a small test program (compiled with mingw on Linux), I noticed that one cannot use the read
and write
calls on the socket fd as obtained using Winsock2's implementation of the socket
call. The write call returns <0 and sets errno=EBADF.
Think of programs run from xinetd, minus their assumption that their stdin/stdout always is a socket. (Some programs do call getpeername for example, which will fail if it is not a socket, subsequently they may exit prematurely.)
So how are {file descriptor type}-agnostic programs that just read/write from/to stdin/stdout supposed to reasonably work in the win32 environment unless making assumptions about the fd?
Or more simply put, is there some magic function call to be executed to wire up Winsock2 socket fds with the win32 (well, mingw) write
implementation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
read()
和write()
函数是 POSIX I/O 系统调用,而不是套接字 API 调用。MinGW 用于编译到本机 Windows 平台。它不提供 POSIX 环境。
将 MinGW 与 Winsock 结合使用时,您有两个选择:
send()
和recv()
。WriteFile()
和ReadFile()
。The
read()
andwrite()
functions are POSIX I/O system calls, not socket API calls.MinGW is for compiling to the native Windows platform. It does not provide a POSIX environment.
When using MinGW with Winsock, you have two options:
send()
andrecv()
.WriteFile()
andReadFile()
.如果你想要在 Windows 上有任何类型的理智行为,请忘记 mingw。它使用 MSVC++ 标准库,该库甚至无法符合 plain C 标准,更不用说 POSIX 了。遗憾的是 cygwin 有点臃肿,但我会接受这种臃肿作为 Windows 编程的代价,并选择 cygwin。或者,您可以为您编写的每个程序编写 2 个不同版本,可能与 #ifdef 纠缠在一起,以支持 MSVC 和 POSIX...
If you want any kind of sane behavior on Windows, forget about mingw. It uses the MSVC++ standard library, which can't even manage to conform to the plain C standard, much less POSIX. Sadly cygwin is a bit bloated, but I would just accept the bloat as the price of programming for Windows and go with cygwin. Or you can write 2 different versions of every program you write, possibly entangled with
#ifdef
s, to support both MSVC and POSIX...Windows 上的套接字句柄不是文件句柄。您必须使用 Winsock 函数来读取/写入/更改状态。
您也不能与 Windows 中其他类型的句柄一致地使用
select
或其同类。Socket handles on Windows are not file handles. You have to use Winsock functions to read/write/change state.
Neither can you use
select
or its ilk consistently with other types of handles in Windows.