Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get stream of a socket object in c#

Tags:

c#

stream

sockets

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.

like image 356
Fer Avatar asked May 09 '12 15:05

Fer


2 Answers

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);
}
like image 141
Chris Shain Avatar answered Oct 21 '22 03:10

Chris Shain


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

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

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

like image 27
John Smith Avatar answered Oct 21 '22 04:10

John Smith