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. NetworkStream.Write

NetworkStream.Write

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
3 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.
  • E Offline
    E Offline
    Eli Nurman
    wrote on last edited by
    #1

    Hi. Does any one know, is there any limit to the buffer size that could be sent at once with the NetworkStream.Write through a socket?. I'm sending a byte array with a length of 16890, and receiving only a few hundred of them.

    networkStream.Write(buffer, 0, buffer.Length);
    

    Please help. Thanks.

    L 1 Reply Last reply
    0
    • E Eli Nurman

      Hi. Does any one know, is there any limit to the buffer size that could be sent at once with the NetworkStream.Write through a socket?. I'm sending a byte array with a length of 16890, and receiving only a few hundred of them.

      networkStream.Write(buffer, 0, buffer.Length);
      

      Please help. Thanks.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I think you can send as much as you like, if not the Write method should/will throw an exception; however when the network (hardware definition and/or protocol) is somehow limited, the protocol stack and/or driver will chop it into several packets. Example: Ethernet packets are limited to some 1500 bytes (this relates to the collision detect mechanism, the maximum length of a connection, and the bit rates). I doubt a receiver will put them together again, so sending 16890 bytes in a single write may result in some 11 packets being transmitted and received. That is one of the reasons a Read method always returns the numbers actually read. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Love, happiness and fewer bugs for 2009!


      modified on Sunday, January 11, 2009 6:55 PM

      modified on Friday, June 10, 2011 12:34 PM

      B 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I think you can send as much as you like, if not the Write method should/will throw an exception; however when the network (hardware definition and/or protocol) is somehow limited, the protocol stack and/or driver will chop it into several packets. Example: Ethernet packets are limited to some 1500 bytes (this relates to the collision detect mechanism, the maximum length of a connection, and the bit rates). I doubt a receiver will put them together again, so sending 16890 bytes in a single write may result in some 11 packets being transmitted and received. That is one of the reasons a Read method always returns the numbers actually read. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Love, happiness and fewer bugs for 2009!


        modified on Sunday, January 11, 2009 6:55 PM

        modified on Friday, June 10, 2011 12:34 PM

        B Offline
        B Offline
        Ben Fair
        wrote on last edited by
        #3

        To expand on Luc's excellent information, the opposite is also true of the data being spread across multiple packets. If multiple pieces of data are sent that are small, depending on how the OS decides to use it's internal buffers, it may combine one of more and send them as 1 packet. So, you can see that you have to have some way to sort it out on the receiving end since it may be received completely differently than how it was sent. One approach is to create your own mechanism, or protocol, for transmitting data. A common technique is to perform 2 transmissions, the first is a small amount of data that contains the size of the data to be sent (can be just 4 bytes representing an Int32). And then the second transmission is the actual data. So, on the client end the first transmission would tell the client to expect N bytes of data. Then the client can read until all N bytes are received.

        Keep It Simple Stupid! (KISS)

        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