Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

c - winsock not supporting read/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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 #ifdefs, to support both MSVC and POSIX...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...