Recommended Posts

Hello, I''m used to writing network programs in unix using read() and write(). Most of the code I see for windows uses recv() and send(). I''m using TCP/IP.
I tried using read() from io.h, but it returns -1.
Can I even use read() and write() in windows for network code? I''m reluctant to use recv() and send() because it seems to be dealing with socket layer stuff and I''m assuming it eventually calls read() and write() (or something similar) to access the transport layer.
Can anyone clarify this issue? What are the pros/cons about using one or the other? I won''t be using any of the special flags for recv() and send(). Thanks.

Share this post

Link to post

Share on other sites

read() and write() are the unix system calls for accessing a file desciptor. The analagous system calls in Windows are ReadFile() and WriteFile(), which do work on sockets. However, using ReadFile() and WriteFile() on a socket is actually more annoying (IMO) than using send() and recv().

From my understanding, _read() and _write() (I''m assuming you''re using MSVC) from the io.h header work in conjunction with the compatibility layer for posix file descriptors, which doesn''t play happily with the Winsock socket functions. (Basically socket functions create socket handles, which are disjoint from the file descriptor system.)

And I''m not understanding your objection to using send() and recv() in the first place.

Share this post

Link to post

Share on other sites

quote:Original post by canuckle I like the unix idea of having a generic file descriptor abstraction, but I guess it doesn''t work that way in Windows for some reason or another.

Actually, Windows has the same kind of abstraction, but instead of having everything abstracted as a file descriptor, everything is abstracted as a handle: pipes, files, serial ports, etc. Which is why ReadFile() and WriteFile() work on sockets.