why TcpClient.GetStream(), not just a property? Will exception go away next time?

  • Thread starter Thread starter Ryan Liu
  • Start date Start date
R

Ryan Liu

Hi,

Why TcpClient has a method TcpClient.GetStream(), not just a read only
property?

By implementing it as a method, does that mean, each time GetStream() could
return a different stream? In other words, is that safe I only call
GetStream() once and set it to a class' variable and reuse it later?

And sometime I see TcpClient.GetStream() throws
System.InvalidOperationException: Operation not allowed on non-connected
sockets. Then I will force the user to log out.

Now I wonder is this "temporary" exception? Maybe

1: I save the stream in class variable, next time I just call Read and
wirte method, do not call GetStream() first, the exception will not occure.

2: If I catch this exception, ignore it, next time, GetStream() or
ReadByte(),Write() will work just fine.

Can someone tell if my guess right or not? It is difficult to test, since
this exception will not always occure.

Thanks,
Ryan
 
Hi,

Ryan Liu said:
Hi,

Why TcpClient has a method TcpClient.GetStream(), not just a read only
property?

Frankly I have no a precise answer for this.
By implementing it as a method, does that mean, each time GetStream()
could
return a different stream? In other words, is that safe I only call
GetStream() once and set it to a class' variable and reuse it later?

No, it will ALWAYS return the same stream, or exception if Connect was not
called before.
And sometime I see TcpClient.GetStream() throws
System.InvalidOperationException: Operation not allowed on non-connected
sockets. Then I will force the user to log out.

Now I wonder is this "temporary" exception? Maybe

The TcpClient is not connected to a remote location.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top