get stream of a socket object in c#

c#, sockets, stream

Solution

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

Problem

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.

Original source