在 C# 中获取套接字对象的流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10519221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:10:31  来源:igfitidea点击:

get stream of a socket object in c#

c#socketsstream

提问by Fer

I have a client-server application which communicates over TCP/IP.
I use System.Net.Sockets.Sockettype 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.Streamtype object. So i need to get Streamobject of my Socketobject.
How can i do that?
Thank you.

我有一个通过 TCP/IP 进行通信的客户端-服务器应用程序。
我使用System.Net.Sockets.Socket类型对象通过 TCP 进行 ascnyronous 通信。基本上我打开连接发送/接收数据并关闭连接。而我的实现是基于 Socket 类型的对象。
现在我需要使用第三方 dll 来做一些事情。这个 dll 需要一个System.IO.Stream类型对象。所以我需要获取我的Socket对象的Stream对象。 我怎样才能做到这一点? 谢谢你。

采纳答案by Chris Shain

Pretty simple really. The constructor for the NetworkStream class accepts a socket to wrap:

真的很简单。NetworkStream 类的构造函数接受要包装的套接字:

http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx

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);
}

回答by John Smith

Try looking at System.Net.Sockets.SocketType.Stream ?

试试看 System.Net.Sockets.SocketType.Stream ?

Or try looking at the System.Net.Sockets.NetworkStream ?

或者尝试查看 System.Net.Sockets.NetworkStream ?

http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx

http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.aspx