suckerons suckerons - 25 days ago 5
C# Question

Using GetStream Method in Stream Object C#

I have implemented a Tcp socket client successfully using:

TcpClient tcpClient = new TcpClient(Endpoint);
tcpClient.Connect("", 3000);
NetworkStream networkStream = tcpClient.GetStream();

Now I am trying to make use the More general Socket class, but I cannot find a method that returns network stream like GetStream

Socket socketClient = new Socket(Endpoint);
socketClient.Connect("", 3000);
NetworkStream networkStream = socketClient.?????

Answer Source

Use NetworkStream to get the Stream:

using(var stream = new NetworkStream(socketClient))
      //use the stream here

If you want to avoid using clause as you mentioned in comments then just don't use it and you can close the stream yourself, just like you said:

var stream = new NetworkStream(socketClient);