364,033 Members | 4770 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

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

Ryan Liu
P: n/a
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


Jun 9 '06 #1
Share this Question
Share on Google+
1 Reply


Ignacio Machin \( .NET/ C# MVP \)
P: n/a
Ignacio Machin \( .NET/ C# MVP \)
Hi,

"Ryan Liu" <ad50275324@online.sh.cn> wrote in message
news:uI4ivQ6iGHA.1936@TK2MSFTNGP04.phx.gbl...[color=blue]
> Hi,
>
> Why TcpClient has a method TcpClient.GetStream(), not just a read only
> property?[/color]

Frankly I have no a precise answer for this.
[color=blue]
> 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?[/color]

No, it will ALWAYS return the same stream, or exception if Connect was not
called before.
[color=blue]
> 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[/color]

The TcpClient is not connected to a remote location.



--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Jun 9 '06 #2

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp