Click here to Skip to main content
15,796,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Visual Studio 2019, VB.Net. Just getting back into coding after many years and thought I would start by setting up a simple chat app in VB.Net. I've downloaded many examples of UDP and TCP/IP, including here, from Code Project, and all have the same error - Specified argument was out of the range of valid values."Parameter name: size", in the following line: serverStream.Read(inStream, 0, buffSize)

Private Sub getMessage()
        For infiniteCounter = 1 To 2
            infiniteCounter = 1
            serverStream = clientSocket.GetStream()
            Dim buffSize As Integer
            Dim inStream(10024) As Byte
            buffSize = clientSocket.ReceiveBufferSize
            serverStream.Read(inStream, 0, buffSize)
            Dim returndata As String =
            readData = "" + returndata
    End Sub

Every example I've tried has the same error, even the example from Microsoft.

What I have tried:

From various sites it seems that the ReceiveBufferSize is different from serverStream Length. However, I can't seem to find a resolution to the problem. Many sites say it shouldn't be coded like that but don't off a fix for it. I'm not understanding why every example fails in the same way. What am I doing wrong? Any help would be appreciated.
Updated 26-Jun-23 11:42am
Richard MacCutchan 26-Jun-23 15:11pm    
The first thing to do is step through this code with the debugger and see what actual value is being returned into buffSize. That should give an idea as to why the following call is failing.
Ralf Meier 26-Jun-23 15:36pm    
when reading your code I stuck at the dimension of the array - did you really want to have it ten thousand bytes large ? or do you originally want to size it 1024 bytes ?
Member 15070471 26-Jun-23 18:50pm    
Thank you Richard, that game me the correct value

1 solution

RecieveBufferSize is not the same as the number of bytes in the buffer itself: it's likely to be considerably bigger.
So when you try to read RecieveBufferSize into your array:
Dim inStream(10024) As Byte
buffSize = clientSocket.ReceiveBufferSize
serverStream.Read(inStream, 0, buffSize)
It looks at the siize of the buffer and the number of bytes you asked it to read and throws an error because one will not fit into the other.
Use the size of your array, and it should go away.
Share this answer
Member 15070471 26-Jun-23 18:48pm    
Thank you. On running the code and checking the error, the number of bytes in the buffer was 65536.
Changing the RecieveBufferSize to 65536 fixed the problem. Still not sure why every example I downloaded had the same error though. Perhaps a chnage in .Net from older versions or something?
OriginalGriff 27-Jun-23 0:37am    
No, that's the wrong approach - you pass the length of the barray you want to recieve in, not the max length of the buffer that supplies the data!
Your way is like fitting a fuel tank to your car that is the size of the underground tank at the gas station, and always trying to fill it completely! It may work, but you end up with a huge tank of fuel attached to the roof of your car with a dribble of fuel in it because the pump will only allow you £100 at a time ...

Ask for what is in the buffer, not what could be in the buffer!
Member 15070471 27-Jun-23 7:07am    
lol, thank you, I've never really understood the relationship between the two, but your analogy makes it clear.
OriginalGriff 27-Jun-23 7:30am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900