Results 1 to 3 of 3

Thread: Buffer size

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    183

    Buffer size

    I'm learning about the networking aspects of .NET for the first time, and I have a question on buffer size. What impact does the size have? The example below from MSDN uses 1024 while some other examples I've seen use different sizes. Would a smaller buffer retrieve smaller chunks of data at once?

    Code:
                Dim handler As Socket = listener.Accept()
    
                data = Nothing
    
                ' An incoming connection needs to be processed.
                While True
                    bytes = New Byte(1024) {}
                    Dim bytesRec As Integer = handler.Receive(bytes)
                    data += Encoding.ASCII.GetString(bytes, 0, bytesRec)
                    If data.IndexOf("<EOF>") > -1 Then
                        Exit While
                    End If
                End While

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Buffer size

    At a lower level, each TCP socket has its own buffer where received bytes are stored.
    Whenever you call the Receive method and pass your own buffer to it, it'll fill up your buffer as much as possible with the data from its internal buffer. So yes, the larger your buffer is the more data you'll be able to "scoop" from the internal buffer at once.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2008
    Posts
    183

    Re: Buffer size

    Thanks! Now I have another question as to why this code isn't working. I'm trying to display the numbers 0 through 20 in the server window. It does so successfully but not the way I want it. I want each number sent as its own stream. My code sends it all at once at stream.Close.

    Do I have to close the stream on each loop? When I try that, the socket also closes. So I have to reopen that connection each time as well?

    Thanks

    Code:
        Sub Main()
            Dim stream As NetworkStream
            Dim streamwriter As IO.StreamWriter
            Dim sender As New TcpClient("localhost", 12345)
    
    
            stream = sender.GetStream()
            streamwriter = New IO.StreamWriter(stream)
    
            Console.WriteLine("Connected to server!")
    
    
            For i = 0 To 20
                streamwriter.WriteLine(i)
                streamwriter.Flush()
                Threading.Thread.Sleep(500)
            Next
            stream.Close()
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width