简体   繁体   English

指定的参数超出范围C#

[英]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#的新手,所以我不知道为什么在Windows 7中我的应用程序(程序)可以正常运行,并且在Windows Server 2012上,当我尝试从客户端获取消息时出现此错误。 C#. C#。

Error: 错误:

system.argumentOutOfRangeException: Specified argument was out of the range of valid values. system.argumentOutOfRangeException:指定的参数超出有效值范围。 Parameter name: Size. 参数名称:大小。 at system.net.sockets.networkStream.Read(Byte[] buffer, int32 offset, int32 size) 在system.net.sockets.networkStream.Read(Byte []缓冲区,int32偏移量,int32大小)

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 也许在这里: (int)clientSocket.ReceiveBufferSize

How to fix this, have any ideas? 如何解决这个问题,有什么想法吗?

I only had to deal with TCP Sockets now, and I found this sample code, and got the same error message. 我现在只需要处理TCP套接字,就找到了此示例代码,并得到了相同的错误消息。 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. 并且,如果在客户端遇到类似的错误,只需检查相同的.Read()部分。 Apply the same solution of using the .Length property. 应用使用.Length属性的相同解决方案。

Why do you cast 你为什么要投

clientSocket.ReceiveBufferSize

to an int when it normally should already be an int? 到一个int,通常它应该已经是一个int了? ( http://msdn.microsoft.com/de-de/library/system.net.sockets.socket.receivebuffersize(v=vs.110).aspx )? 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. 它至少应为ReceiveBufferSize的值。 But be sure that the value does not change between initializing an the Read(). 但是请确保在初始化Read()之间该值不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM