suckerons
suckerons

Reputation: 17

Using GetStream Method in Stream Object C#

I have implemented a Tcp socket client successfully using:

TcpClient tcpClient = new TcpClient(Endpoint);
tcpClient.Connect("127.0.0.1", 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("127.0.0.1", 3000);
NetworkStream networkStream = socketClient.?????

Upvotes: 1

Views: 2670

Answers (1)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34150

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

Upvotes: 1

Related Questions