Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

basic question about NetworkStream( read/write, multiple thread)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ryan Liu - 02 Apr 2008 05:53 GMT
Hi, I have some basic question about NetworkStream, can someone explain to
me? Thanks a lot in advance!

TcpClient has a GetStream() method return a NetworkStream for read and
write.

I remember there are 2 streams in Java, one for read, one for write.

If I just use synchronous Read() and Write() method:

If I have two threads, one reads, and another writes to the stream, and I
don't use any lock mechanism, will read/write operations overlap and
conflict each other, and raise exception? Or it internally has a way to
gurantee the safety?

And if I have two write threads, and there is no lock mechanism, will two
write operations interfere each other, and the receiver get mixed data?

If I use BeginRead(), BeginWrite() asynchronously in multiple threads, will
that solve the problems above(if any)? And  is it safe not to use my own
lock mechanism in asynchronous mode?

Thanks again!
Ryan Liu
Thanks a lot!
Peter Duniho - 02 Apr 2008 07:50 GMT
> Hi, I have some basic question about NetworkStream, can someone explain  
> to
[quoted text clipped - 4 lines]
>
> I remember there are 2 streams in Java, one for read, one for write.

I haven't tried this, but I believe that you can create multiple  
NetworkStreams from the same Socket instance.  Then you could just always  
use one stream to read and one to write, similar to the "Input..." and  
"Output..." streams in Java.

> If I just use synchronous Read() and Write() method:
>
> If I have two threads, one reads, and another writes to the stream, and I
> don't use any lock mechanism, will read/write operations overlap and
> conflict each other, and raise exception? Or it internally has a way to
> gurantee the safety?

NetworkStream is documented as _not_ being thread safe.  Now, whether this  
means you'd actually run into trouble doing that, I can't say.  The fact  
is, the NetworkStream should basically just be delegating to the  
underlying Socket, and the Socket class _is_ thread safe.  Furthermore,  
simultaneous reads and writes on a Socket are safe.

So I think it's possible that the NetworkStream would in fact work fine.  
That said, the semantics of the NetworkStream and a TCP Socket are so very  
similar to each other, it seems to me that it'd be better to just use a  
Socket in this case where you want to read and write from different  
threads.  That way you could be certain.

> And if I have two write threads, and there is no lock mechanism, will two
> write operations interfere each other, and the receiver get mixed data?

Now _this_ is definitely something you need to worry about.  Even with the  
thread-safe Socket class, all that means is that you can use the same  
instance from different threads without the operation of the class itself  
breaking.  But there's nothing about TCP that would ensure that  
simultaneous writes from multiple threads would not interfere with each  
other.

In other words, the Socket class will happily allow this and it won't  
break.  But sure, your data very well could wind up mixed up.

Now, that said...I would expect that if you use the synchronous (blocking)  
Send() method on the Socket, you could at least ensure that an individual  
send would complete without other data getting mixed in (it's not supposed  
to return until it's sent all of the data, as long as the Socket is in  
blocking mode, which it would be by default).  But in that case you'd have  
to make sure that you use the synchronous method in blocking mode, you'd  
have to make sure that you send _all_ of the data that needs to stay  
together in a single call to the method, and of course you'd also have to  
manage your own ordering of different chunks that wind up sent (if your  
overall application-level protocol is indifferent to the order of these  
chunks, then you might not care about that last part...it really depends  
on what you're sending though).

> If I use BeginRead(), BeginWrite() asynchronously in multiple threads,  
> will
> that solve the problems above(if any)? And  is it safe not to use my own
> lock mechanism in asynchronous mode?

If there's a problem with calling Read() and Write() at the same time,  
there could easily be a problem calling the asynchronous versions at the  
same time too.  Keep in mind that the thread safety isn't necessarily just  
about the network i/o itself...it's about the internal consistency of the  
class's state.  With NetworkStream being documented as not thread safe,  
there's a theoretical possibility that it's not even safe to call  
BeginRead() and BeginWrite() at the same time.

Granted, I suspect that in that specific case it actually is safe.  But I  
don't have anything that would actually prove that.  Again, it seems to me  
you'd be better off just using the Socket class if you want to do the  
network i/o in a thread-safe manner.

Pete
Arne Vajhøj - 09 Apr 2008 03:34 GMT
>> Hi, I have some basic question about NetworkStream, can someone
>> explain to
[quoted text clipped - 9 lines]
> always use one stream to read and one to write, similar to the
> "Input..." and "Output..." streams in Java.

I would expect 2 calls to GetStream to return references to the
same object making it rather pointless.

Arne
Peter Duniho - 09 Apr 2008 03:44 GMT
> I would expect 2 calls to GetStream to return references to the
> same object making it rather pointless.

"GetStream"?  I don't know what method you're talking about, but I was  
referring to creating two NetworkStreams from scratch, using a constructor  
that takes a Socket as a parameter.  As far as I know, using the "new"  
statement you're guaranteed to get two different instances if you call it  
twice (I suppose it could have the same special-case handling the String  
class has, but it would surprise if that was the case).

Pete
Arne Vajhøj - 13 Apr 2008 22:27 GMT
>> I would expect 2 calls to GetStream to return references to the
>> same object making it rather pointless.
>
> "GetStream"?  I don't know what method you're talking about,

It is a method in TcpClient.

Quote from original post:

#TcpClient has a GetStream() method return a NetworkStream for read and
#write.

>                                                             but I was
> referring to creating two NetworkStreams from scratch, using a
> constructor that takes a Socket as a parameter.

If he rewrite from using TcpClient to use Socket he can get two
NetworkStreams out of it.

Arne
Peter Duniho - 13 Apr 2008 22:53 GMT
> [...]
> If he rewrite from using TcpClient to use Socket he can get two
> NetworkStreams out of it.

Which is just what I wrote.

I really don't understand what your point is.  You're probably right that  
calling TcpClient.GetStream() twice is pointless.  But I never suggested  
doing that.  My post (and even the part you quoted, for that matter)  
specifically suggested using a Socket to create the NetworkStream  
instances.

What's the relevance of your statement?

Pete
Arne Vajhøj - 13 Apr 2008 23:31 GMT
>> If he rewrite from using TcpClient to use Socket he can get two
>> NetworkStreams out of it.
[quoted text clipped - 8 lines]
>
> What's the relevance of your statement?

That it relates to the original post ...

Arne
Peter Duniho - 13 Apr 2008 23:52 GMT
>>  What's the relevance of your statement?
>
> That it relates to the original post ...

There are a great number of things that are related to the original post  
and yet which would be pointless with respect to addressing the original  
post.

Why would you pick that one thing as _the_ pointless thing you would  
mention?  Why would you bother to mention _any_ pointless thing?  If you  
are mentioning pointless things, why stop at just the one?  Why not  
describe all of the pointless things you can think of?  And why would you  
do so as a reply to my own post (to which by your own admission your reply  
wasn't relevant), rather than to the original post?

It's funny: it's pretty obvious that you misread my post and eager to try  
to describe my own suggestion as "pointless", replied to my post and did  
just that.  Now that you realize you were mistaken, it's impossible for  
you to admit it, inasmuch as it's impossible for you to admit _any_  
mistake.

I guess the really curious thing is whether you even realize this is going  
on inside your head.  From outside, it's readily apparent.  If you don't  
recognize it yourself, that's sad.  Though, to your credit: in my country,  
that'd qualify you for the job of President.  So, you've got that going  
for you.  :)

Pete
Arne Vajhøj - 09 Apr 2008 03:31 GMT
> TcpClient has a GetStream() method return a NetworkStream for read and
> write.
>
> I remember there are 2 streams in Java, one for read, one for write.

You can use the same Stream for both read and write.

> If I just use synchronous Read() and Write() method:
>
> If I have two threads, one reads, and another writes to the stream, and I
> don't use any lock mechanism, will read/write operations overlap and
> conflict each other, and raise exception? Or it internally has a way to
> gurantee the safety?

I am 99.9% sure that it will work fine without lock, because the two
operations should be independent.

But you should of course check the docs.

> And if I have two write threads, and there is no lock mechanism, will two
> write operations interfere each other, and the receiver get mixed data?

Here I would expect problem unless it is written in large bold blinking
font in the docs that Write does a lock.

Arne

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.