Socket.Send 和 Stream.Write 有什么区别?(关于 tcp ip 连接) [英] What is the difference between Socket.Send and Stream.Write? (in relation to tcp ip connections)

查看:81
本文介绍了Socket.Send 和 Stream.Write 有什么区别?(关于 tcp ip 连接)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理服务器/客户端连接时,我已经看到这两种方法都被有效地使用,但没有任何明显的优势,但我怀疑如果其中一个没有任何已知的优势,它们都会存在.有谁知道两者之间有什么明显的区别?非常感谢您的帮助,谢谢.

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.

推荐答案

Socket.Send 是直接通过 WINSOCK 层的原始数据发送... Stream 缓冲区并在您发送数据时对其进行处理.它通常用于需要类似流的功能的情况.显然,那里有一些额外的开销.与 Send 相比,这种开销包括创建一个新类来管理流"并在您和 Socket.Send 之间引入几个间接层.NetworkStream.Write 最终只调用 Socket.Send;但这是几层开销.

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 to Send, this overhead includes creating a new class to manage a "stream" and introducing a couple of layers of indirection between you and Socket.Send. NetworkStream.Write eventually just calls Socket.Send; but that's a couple of layers of overhead down.

通过使用 NetworkStream 类型,还需要做更多的工作来设置流.

There's also a bit more work to get the stream set up, through the use of the NetworkStream type.

当然,流也可以是双向的,这意味着您可以读取和写入它.您可以使用 Receive 方法从套接字中读取数据.

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.

使用 SendReceive 直接将您连接到套接字.如果您使用 Stream 派生类,您将与 Socket 分离,并且可以自由地使用与 Stream 一起工作的方法/类.

Use of Send or Receive directly couples you to socket. If you use a Stream-derivative, you're decoupled from Socket and free to use methods/classes that work with Stream.

这篇关于Socket.Send 和 Stream.Write 有什么区别?(关于 tcp ip 连接)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆