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

R

Ryan Liu

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!
 
A

Arne Vajhøj

Ryan said:
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
 
A

Arne Vajhøj

Peter said:
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.

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

Arne
 
A

Arne Vajhøj

Peter said:
"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
 
A

Arne Vajhøj

Peter said:
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?

That it relates to the original post ...

Arne
 

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

Top