在 C# 中获取套接字对象的流 [英] get stream of a socket object in c#
问题描述
我有一个通过 TCP/IP 通信的客户端-服务器应用程序.
我使用 System.Net.Sockets.Socket
类型对象通过 TCP 进行 ascnyronous 通信.基本上我打开连接发送/接收数据并关闭连接.而我的实现是基于 Socket 类型的对象.
现在我需要使用第三方 dll 来做一些事情.这个 dll 需要一个 System.IO.Stream
类型的对象.所以我需要获取我的 Socket 对象的 Stream 对象.
我该怎么做?
谢谢.
I have a client-server application which communicates over TCP/IP.
I use System.Net.Sockets.Socket
type object for ascnyronous communication over TCP. Basicly i open connection send/receive data and close connection. And my implementation is based on Socket type objects.
Now i need to use a third-party dll to do something. This dll expects a System.IO.Stream
type object. So i need to get Stream object of my Socket object.
How can i do that?
Thank you.
推荐答案
其实很简单.NetworkStream 类的构造函数接受要包装的套接字:
Pretty simple really. The constructor for the NetworkStream class accepts a socket to wrap:
http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx
// NOTE: This demonstrates disposal of the stream when you are
// done with it- you may not want that behavior.
using (var myStream = new NetworkStream(mySocket)) {
my3rdPartyObject.Foo(myStream);
}
这篇关于在 C# 中获取套接字对象的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!