In dealing with server/client connections, I have seen both of these used effectively without any apparent advantages to either, yet I doubt they would both exist if there weren't any known advantages to one or the other. Does anyone know any clear differences between the two? Help would be much appreciated, thanks.
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
Socket.Send
is a raw send of data directly through the WINSOCK layer...Stream
buffers and processes data as you send it. It's generally used for situations where you need stream-like functionality. There's some extra overhead there, obviously. In comparison toSend
, this overhead includes creating a new class to manage a "stream" and introducing a couple of layers of indirection between you andSocket.Send
.NetworkStream.Write
eventually just callsSocket.Send
; but that's a couple of layers of overhead down.There's also a bit more work to get the stream set up, through the use of the
NetworkStream
type.If course, a stream can also be bidirectional, meaning you can both read and write to it. You can read from a socket perfectly fine with the
Receive
methods.Use of
Send
orReceive
directly couples you to socket. If you use aStream
-derivative, you're decoupled fromSocket
and free to use methods/classes that work withStream
.