Does socket has 2 streams ? What is "(a)synchronous"? Will BeginReceive() creates too many threads so hurt performance?

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

    Does socket has 2 streams ? What is "(a)synchronous"? Will BeginReceive() creates too many threads so hurt performance?

    TcpClient has a method called GetworkStream GetStream();
    So in other words, there is only one stream associate with it for input and
    output, right?

    So while it is receiving, it can not send, and vise visa, right? So will it
    be a problem both server and client can initiative a sending action?

    TcpClient only supports synchronous operation. What does "Synchronou s" mean?
    Means while it is reading or waiting for data to arrive from the stream, it
    will block current thread? Does it also mean it will block another thread
    in the same process from sending data though this stream? Or just an
    exception while other thread sends data.

    But of cause it can not block the computer on the another side of stream,
    what happen another side client computer call Steam's read method?

    Socket does not has such method like GetStream(), but it supports
    asynchronous operation. Does this means it has 2 streams for input and
    output?

    And what does "asynchrono us" mean? If I have a server keep receiving
    message from clients, if I use BeginReceive() method, is that means in the
    endless loop, it will create a thread each time when it executes
    BeginReceive(). Creating so many threads will it hurt performance of the
    server?

    And is that true, I need an endless loop to receive all messages from one
    client, and if each message is long, just to receive a single message
    (message has the header to tell the total length), it might need get data
    from multiple BeginReceive()? And data get from one BeginReceive() might
    has first part belong to previous message and the rest for next message?

    On server side, I think to minimize the resource usage, it is better just
    keep one thread for each client. This thread will receive data from client,
    and when one complete message received, it process this message in the same
    thread. Is this reasonable?

    But if multiple BeginReceive() get one message, each BeginReceive() creates
    a thread, then this is really different from my intention to reduce the
    resource usage.

    Sever programming is a difficult task for me. I've asked too many questions.
    Really appreciate for your help!

    Ryan




  • William Stacey [MVP]

    #2
    Re: Does socket has 2 streams ? What is "(a)synchr onous"? Will BeginReceive() creates too many threads so hurt performance?

    | So in other words, there is only one stream associate with it for input
    and
    | output, right?

    right.

    | So while it is receiving, it can not send, and vise visa, right? So will
    it
    | be a problem both server and client can initiative a sending action?

    You can read and write a ~same time. You could, for example, have 1 thread
    reading and 1 thread writing. You don't want multiple readers or writers on
    the same socket however.

    | Socket does not has such method like GetStream(), but it supports
    | asynchronous operation.

    NetworkStream has BeginRead and BeginWrite also.

    | Does this means it has 2 streams for input and
    | output?

    No stream at all. However you can send and receive on diff threads as same
    time. Internally, it may lock (have not seen the code) any shared data
    structures so it may not allow "actual" simultaneous reads and writes, but
    close enouph.

    | And what does "asynchrono us" mean? If I have a server keep receiving
    | message from clients, if I use BeginReceive() method, is that means in the
    | endless loop, it will create a thread each time when it executes
    | BeginReceive(). Creating so many threads will it hurt performance of the
    | server?

    The callback is run on a thread pool thread and those are shared and max
    threads can only grow so high.


    Comment

    • Ryan Liu

      #3
      Re: Does socket has 2 streams ? What is "(a)synchr onous"? Will BeginReceive() creates too many threads so hurt performance?

      Hi William,

      Thanks a lot for your prompt and very clear reply.

      One last question, I have defined Message data structure, and it has method
      to convert a Message to byte[] and parse byte[] back to Message.

      If client send the whole Message's byte[] in one BeginSend(), will the
      server guaranteed to receive all bytes if the receiving buffer is big
      enough, so I don't have to concatenate the bytes received on the server.

      The message has the first 4 bytes indicates start of message , followed by 4
      bytes to define total length, can these information be helpful in
      BeginReceive()?

      I know these questions are too detailed and specific, really greatful if you
      can help.

      Thanks a lot!
      Ryan

      "William Stacey [MVP]" <william.stacey @gmail.com> дÈëÓʼþ
      news:eltGQwUUGH A.4384@tk2msftn gp13.phx.gbl...[color=blue]
      > | So in other words, there is only one stream associate with it for input
      > and
      > | output, right?
      >
      > right.
      >
      > | So while it is receiving, it can not send, and vise visa, right? So will
      > it
      > | be a problem both server and client can initiative a sending action?
      >
      > You can read and write a ~same time. You could, for example, have 1[/color]
      thread[color=blue]
      > reading and 1 thread writing. You don't want multiple readers or writers[/color]
      on[color=blue]
      > the same socket however.
      >
      > | Socket does not has such method like GetStream(), but it supports
      > | asynchronous operation.
      >
      > NetworkStream has BeginRead and BeginWrite also.
      >
      > | Does this means it has 2 streams for input and
      > | output?
      >
      > No stream at all. However you can send and receive on diff threads as[/color]
      same[color=blue]
      > time. Internally, it may lock (have not seen the code) any shared data
      > structures so it may not allow "actual" simultaneous reads and writes, but
      > close enouph.
      >
      > | And what does "asynchrono us" mean? If I have a server keep receiving
      > | message from clients, if I use BeginReceive() method, is that means in[/color]
      the[color=blue]
      > | endless loop, it will create a thread each time when it executes
      > | BeginReceive(). Creating so many threads will it hurt performance of the
      > | server?
      >
      > The callback is run on a thread pool thread and those are shared and max
      > threads can only grow so high.
      >
      >[/color]


      Comment

      • William Stacey [MVP]

        #4
        Re: Does socket has 2 streams ? What is &quot;(a)synchr onous&quot;? Will BeginReceive() creates too many threads so hurt performance?

        For udp, you will get the whole message. For tcp, it is a stream, so you
        can only expect to get 1 or more bytes (or 0 if client shutdown send side).
        There is no notion of message boundaries other then what you define in your
        protocol. So in this case, you read 4 bytes and get the int len. Then read
        util you get all bytes in the message. After you get the len, you can
        create one byte[] of that size and use that as your buffer for the whole
        message, so you don't have to append smaller byte[]'s together.

        --
        William Stacey [MVP]

        "Ryan Liu" <ad50275324@onl ine.sh.cn> wrote in message
        news:e8H%23p9UU GHA.2156@tk2msf tngp13.phx.gbl. ..
        | Hi William,
        |
        | Thanks a lot for your prompt and very clear reply.
        |
        | One last question, I have defined Message data structure, and it has
        method
        | to convert a Message to byte[] and parse byte[] back to Message.
        |
        | If client send the whole Message's byte[] in one BeginSend(), will the
        | server guaranteed to receive all bytes if the receiving buffer is big
        | enough, so I don't have to concatenate the bytes received on the server.
        |
        | The message has the first 4 bytes indicates start of message , followed by
        4
        | bytes to define total length, can these information be helpful in
        | BeginReceive()?
        |
        | I know these questions are too detailed and specific, really greatful if
        you
        | can help.
        |
        | Thanks a lot!
        | Ryan
        |
        | "William Stacey [MVP]" <william.stacey @gmail.com> дÈëÓʼþ
        | news:eltGQwUUGH A.4384@tk2msftn gp13.phx.gbl...
        | > | So in other words, there is only one stream associate with it for
        input
        | > and
        | > | output, right?
        | >
        | > right.
        | >
        | > | So while it is receiving, it can not send, and vise visa, right? So
        will
        | > it
        | > | be a problem both server and client can initiative a sending action?
        | >
        | > You can read and write a ~same time. You could, for example, have 1
        | thread
        | > reading and 1 thread writing. You don't want multiple readers or
        writers
        | on
        | > the same socket however.
        | >
        | > | Socket does not has such method like GetStream(), but it supports
        | > | asynchronous operation.
        | >
        | > NetworkStream has BeginRead and BeginWrite also.
        | >
        | > | Does this means it has 2 streams for input and
        | > | output?
        | >
        | > No stream at all. However you can send and receive on diff threads as
        | same
        | > time. Internally, it may lock (have not seen the code) any shared data
        | > structures so it may not allow "actual" simultaneous reads and writes,
        but
        | > close enouph.
        | >
        | > | And what does "asynchrono us" mean? If I have a server keep receiving
        | > | message from clients, if I use BeginReceive() method, is that means in
        | the
        | > | endless loop, it will create a thread each time when it executes
        | > | BeginReceive(). Creating so many threads will it hurt performance of
        the
        | > | server?
        | >
        | > The callback is run on a thread pool thread and those are shared and max
        | > threads can only grow so high.
        | >
        | >
        |
        |


        Comment

        • Nick Hounsome

          #5
          Re: Does socket has 2 streams ? What is &quot;(a)synchr onous&quot;? Will BeginReceive() creates too many threads so hurt performance?


          "Ryan Liu" <ad50275324@onl ine.sh.cn> wrote in message
          news:e8H%23p9UU GHA.2156@tk2msf tngp13.phx.gbl. ..[color=blue]
          > Hi William,
          >
          > Thanks a lot for your prompt and very clear reply.
          >
          > One last question, I have defined Message data structure, and it has
          > method
          > to convert a Message to byte[] and parse byte[] back to Message.
          >
          > If client send the whole Message's byte[] in one BeginSend(), will the
          > server guaranteed to receive all bytes if the receiving buffer is big
          > enough, so I don't have to concatenate the bytes received on the server.
          >[/color]

          No it isn't gauranteed and you should not rely on it. TCP is a stream
          oriented protocol not a message oriented protocol.
          [color=blue]
          > The message has the first 4 bytes indicates start of message , followed by
          > 4
          > bytes to define total length, can these information be helpful in
          > BeginReceive()?[/color]

          Yes but you can't even gaurantee the 4 bytes or the length will come
          together in all cases - it is possible (assuming your protocol doesn't
          handshake every message) for the sender to send 2 messages one after the
          other such that it is buffered at the sender and then split in the network
          resulting in a partial read of the start of message or the length (It's very
          unlikely but you should really handle it).


          Comment

          Working...