简体   繁体   English

NetworkStream.Write 与 Socket.Send

[英]NetworkStream.Write vs. Socket.Send

I have a c# application that I use a custom FTP library for.我有一个 c# 应用程序,我使用自定义 FTP 库。 Right now Im using Socket.Send to send the data but I was wondering if it would be better to initiate a NetworkStream with the socket and use NetworkStream.Write instead.现在我使用 Socket.Send 发送数据,但我想知道用套接字启动 NetworkStream 并改用 NetworkStream.Write 是否会更好。

Are there any advantages to using one over the other?使用其中一个有什么优势吗?

The advantage of a NetworkStream derives primarily from the fact that it is a Stream . NetworkStream的优势主要源于它是Stream The disadvantage of a Socket is that common code that reads and writes from abstract I/O sources like a Stream cannot handle a Socket . Socket的缺点是从抽象 I/O 源(如Stream )读取和写入的通用代码无法处理Socket

The main use case for a NetworkStream is that you have some code elsewhere that reads or writes from a Stream , and you wish you could use it with a Socket . NetworkStream的主要用例是您在其他地方有一些从Stream读取或写入的代码,并且您希望可以将它与Socket一起使用。 You would know if were in this situation and then NetworkStream would be a big help!您会知道是否处于这种情况,然后NetworkStream将有很大帮助!

Say for example you had a communications library and you supported serializing messages from files, named pipes and TCP/IP.例如,假设您有一个通信库,并且您支持从文件、命名管道和 TCP/IP 序列化消息。 The ideal choice for the I/O class would be Stream . I/O class 的理想选择是Stream Then your serialization methods could accept a FileStream , a PipeStream , or a NetworkStream .然后您的序列化方法可以接受FileStreamPipeStreamNetworkStream It would even accept a MemoryStream .它甚至会接受MemoryStream This is the benefit of abstraction because after we've created the stream, a method can interact with it without knowing what kind of stream it is.这是抽象的好处,因为在我们创建了 stream 之后,一个方法可以与它交互,而无需知道它是哪种 stream。

In this sense, a NetworkStream uses the adapter design pattern.从这个意义上说, NetworkStream使用适配器设计模式。 It adapts the Socket API to the Stream API so that clients that are expecting a Stream can use it.它将Socket API 适配到Stream API 以便期望Stream的客户可以使用它。

So finally, the question, if NetworkStream is a Stream adapter for a Socket , which one should we use?所以最后的问题是,如果NetworkStreamSocketStream适配器,我们应该使用哪一个? Well, if you need a Stream , then NetworkStream is your only choice.好吧,如果您需要Stream ,那么NetworkStream是您唯一的选择。 If you don't need a Stream , then you can use whichever API you are most comfortable with.如果您不需要Stream ,那么您可以使用您最熟悉的 API 。 If you are already using Socket successfully, there is no pressing reason to switch to NetworkStream .如果您已经成功使用了Socket ,则没有迫切的理由切换到NetworkStream

You can potentially separate creation of NetworkStream and to work with that as with abstract Stream - so you'll be able to change your transport or simply to create Stream stubs for testing.您可以单独创建NetworkStream并像使用抽象Stream一样使用它 - 这样您就可以更改传输或简单地创建Stream存根进行测试。

As a question of method itself - NetworkStream.Write inside has the only operation (except state checks) streamSocket.Send(buffer, offset, size, SocketFlags.None);作为方法本身的问题 - NetworkStream.Write里面有唯一的操作(除了 state 检查) streamSocket.Send(buffer, offset, size, SocketFlags.None); - so it's mostly the same as to call that on socket. - 所以它与在套接字上调用它几乎相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM