Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. can TcpClient :: GetStream() be used in separate threads for the same TcpClient ?

can TcpClient :: GetStream() be used in separate threads for the same TcpClient ?

Scheduled Pinned Locked Moved C#
questionsysadmin
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abiemann
    wrote on last edited by
    #1

    my server implements a reading thread that blocks until data is received. what I'd like to do is have a designated thread that only writes as data becomes available (an event would be created that contains the TcpClient in the event argument). Therefore the question: is it possible to use TcpClient.GetStream() twice for the same TcpClient ?

    A P 2 Replies Last reply
    0
    • A abiemann

      my server implements a reading thread that blocks until data is received. what I'd like to do is have a designated thread that only writes as data becomes available (an event would be created that contains the TcpClient in the event argument). Therefore the question: is it possible to use TcpClient.GetStream() twice for the same TcpClient ?

      A Offline
      A Offline
      April Fans
      wrote on last edited by
      #2

      It's not possible. 'tcpclient.getstream() ' can get all btye[] by server socket sending.

      April Comm100 - Leading Live Chat Software Provider

      1 Reply Last reply
      0
      • A abiemann

        my server implements a reading thread that blocks until data is received. what I'd like to do is have a designated thread that only writes as data becomes available (an event would be created that contains the TcpClient in the event argument). Therefore the question: is it possible to use TcpClient.GetStream() twice for the same TcpClient ?

        P Offline
        P Offline
        Paulo Zemek
        wrote on last edited by
        #3

        Yes, it's possible. You can't read or write by two threads, but having a thread that only reads and a thread that only writes is perfectly possible. I always do this but, in fact, I call GetStream only once, and pass the returned stream as the parameter to the threads, but I really think this don't change anything. Also, be carefull not to close the stream before you finished, as closing the stream will also close the tcp/ip connection.

        A 1 Reply Last reply
        0
        • P Paulo Zemek

          Yes, it's possible. You can't read or write by two threads, but having a thread that only reads and a thread that only writes is perfectly possible. I always do this but, in fact, I call GetStream only once, and pass the returned stream as the parameter to the threads, but I really think this don't change anything. Also, be carefull not to close the stream before you finished, as closing the stream will also close the tcp/ip connection.

          A Offline
          A Offline
          abiemann
          wrote on last edited by
          #4

          passing the TcpClient object around and calling GetStream() once in the read thread and once in the write thread worked just fine. Since I found this out I have updated my code to use a secure socket and now I'm passing around a NegotiateStream object (but no need to call GetStream) :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups