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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan Liu

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

    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!


  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: basic question about NetworkStream( read/write, multiple thread)

    Ryan Liu wrote:
    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

    Comment

    • =?UTF-8?B?QXJuZSBWYWpow7hq?=

      #3
      Re: basic question about NetworkStream( read/write, multiple thread)

      Peter Duniho wrote:
      On Tue, 01 Apr 2008 21:53:55 -0700, Ryan Liu <rliu@powercati .comwrote:
      >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.
      >
      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

      Comment

      • =?UTF-8?B?QXJuZSBWYWpow7hq?=

        #4
        Re: basic question about NetworkStream( read/write, multiple thread)

        Peter Duniho wrote:
        On Tue, 08 Apr 2008 19:34:42 -0700, Arne Vajhøj <arne@vajhoej.d kwrote:
        >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

        Comment

        • Peter Duniho

          #5
          Re: basic question about NetworkStream( read/write, multiple thread)

          On Sun, 13 Apr 2008 14:27:00 -0700, Arne Vajhøj <arne@vajhoej.d kwrote:
          [...]
          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.GetSt ream() 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

          Comment

          • =?UTF-8?B?QXJuZSBWYWpow7hq?=

            #6
            Re: basic question about NetworkStream( read/write, multiple thread)

            Peter Duniho wrote:
            On Sun, 13 Apr 2008 14:27:00 -0700, Arne Vajhøj <arne@vajhoej.d kwrote:
            >If he rewrite from using TcpClient to use Socket he can get two
            >NetworkStrea ms 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.GetSt ream() 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

            Comment

            • Peter Duniho

              #7
              Re: basic question about NetworkStream( read/write, multiple thread)

              On Sun, 13 Apr 2008 15:31:32 -0700, Arne Vajhøj <arne@vajhoej.d kwrote:
              > 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

              Comment

              Working...