Click to See Complete Forum and Search --> : Buffer size
ElPresidente408
Sep 1st, 2009, 02:53 PM
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?
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
Atheist
Sep 1st, 2009, 03:20 PM
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.
ElPresidente408
Sep 1st, 2009, 08:47 PM
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
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
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.