Neatek
Neatek

Reputation: 21

Specified argument was out of range C#

i am newbie in C#, so i dont know why in Windows 7 my application(program) works perfectly and on Windows Server 2012 i have this error when i trying to get message from client-side. C#.

Error:

system.argumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: Size. at system.net.sockets.networkStream.Read(Byte[] buffer, int32 offset, int32 size)

Send function:

byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();

Receive function:

NetworkStream networkStream = clientSocket.GetStream();
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Console.WriteLine("From client - " + clNo + " : " + dataFromClient);

Problem in this row:

networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

and maybe here: (int)clientSocket.ReceiveBufferSize

How to fix this, have any ideas?

Upvotes: 2

Views: 4294

Answers (2)

PrfyVdlx
PrfyVdlx

Reputation: 21

I only had to deal with TCP Sockets now, and I found this sample code, and got the same error message. I searched the web, and here's what I found:

Change this:

networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

Into this:

networkStream.Read(bytesFrom, 0, bytesFrom.Length);

And if you are getting a similar error for the Client side, just check on the same .Read() section. Apply the same solution of using the .Length property.

Upvotes: 2

Michael
Michael

Reputation: 37

Why do you cast

clientSocket.ReceiveBufferSize

to an int when it normally should already be an int? (http://msdn.microsoft.com/de-de/library/system.net.sockets.socket.receivebuffersize(v=vs.110).aspx)?

And to what size is

byteFrom

initialized? It should be at least the value of ReceiveBufferSize. But be sure that the value does not change between initializing an the Read().

Upvotes: 0

Related Questions