Archived

This topic is now archived and is closed to further replies.

recv and send vs read and write?

This topic is 4941 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Linux treats sockets and files as the same thing. Windows does not. Send and Recv do not call Read and Write under the hood.

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 SiCrane
And I''m not understanding your objection to using send() and recv() in the first place.


I thought send() and recv() called write() and read() under the hood. So I was hoping to use write() and read() for simplicity and a slightly better performance.

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.

From your posts, it seems that I should be using send() and recv() and give up on the idea of read() and write() (or ReadFile() and WriteFile()). That''s what I''ll do.

Thanks for your help, guys.

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.

Share this post


Link to post
Share on other sites