System.Argument超出异常:指定的参数超出有效值范围

时间:2014-12-20 09:01:21

标签: c# winforms sockets client-server tcp-ip

经过几次修复,我仍然有一个令人讨厌的错误。我使用VS2013的开发人员命令提示符调试并运行我的服务器,并使用VS 2013运行客户端。

首先,我运行我的服务器没有任何问题,但是当我启动客户端时,将数据放在textbox中,然后点击发送问题。

这是服务器端的代码,用于指导问题。

IPAddress ipaddr=IPAddress.Parse("127.0.0.1");

TcpListener serverSocket = new TcpListener(ipaddr,8002);

int requestCount = 0;

TcpClient clientSocket = default(TcpClient);

serverSocket.Start();

Console.WriteLine(" >> Server Started");

clientSocket = serverSocket.AcceptTcpClient();

Console.WriteLine(" >> Accept connection from client");

requestCount = 0;

while ((true))
{
    try
    {
        requestCount = requestCount + 1;

        NetworkStream networkStream = clientSocket.GetStream();

        byte[] bytesFrom = new byte[10025];
        //issue stems out of here although i changed bytes size and tried too
        networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

        string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
        dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

        Console.WriteLine(" >> Data from client - " + dataFromClient);

        string serverResponse = "Last Message from client" + dataFromClient;

        Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);

        networkStream.Write(sendBytes, 0, sendBytes.Length);
        networkStream.Flush();

        Console.WriteLine(" >> " + serverResponse);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

clientSocket.Close();
serverSocket.Stop();

Console.WriteLine(" >> exit");
Console.ReadLine();

编辑:提出建议后,我改变了:

byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);`

byte[] bytesFrom = new byte[clientSocket.ReceiveBufferSize];
networkStream.Read(bytesFrom, 0, bytesFrom.Length);  

导致了屏幕截图,但没有收到客户的数据。

Screenshot

错误(来自命令提示符): -

  

参数名称:System.Net.Sockets.NetworkStream.Read(byte [] buffer,int offset,int size)的大小

     

在Console.Application1.Prog.Main():
  系统参数超出范围:指定参数超出有效值范围。

他们实际上是在呼叫valid value

PS。道歉,如果我提供的代码太长但需要

2 个答案:

答案 0 :(得分:1)

您应将receiveBuffer size指定为第三个参数,而不是clientSocket.ReceiveBufferSize

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

OR

使用clientSocket.ReceiveBufferSize创建缓冲区;

byte[] bytesFrom = new byte[clientSocket.ReceiveBufferSize];
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

以下是来自microsoft的示例:

while(true) 
      {
        Console.Write("Waiting for a connection... ");

        // Perform a blocking call to accept requests.
        // You could also user server.AcceptSocket() here.
        TcpClient client = server.AcceptTcpClient();            
        Console.WriteLine("Connected!");

        data = null;

        // Get a stream object for reading and writing
        NetworkStream stream = client.GetStream();

        int i;

        // Loop to receive all the data sent by the client.
        while((i = stream.Read(bytes, 0, bytes.Length))!=0) 
        {   
          // Translate data bytes to a ASCII string.
          data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
          Console.WriteLine("Received: {0}", data);

          // Process the data sent by the client.
          data = data.ToUpper();

          byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

          // Send back a response.
          stream.Write(msg, 0, msg.Length);
          Console.WriteLine("Sent: {0}", data);            
        }

        // Shutdown and end connection
        client.Close();
      }

答案 1 :(得分:0)

这个问题是很久以前的,但对其他人可能有用。我想建议您在客户端执行相同的操作。您应该如下更改客户端代码的接收部分;

byte[] inStream = new byte[clientSocket.ReceiveBufferSize];

serverStream.Read(inStream, 0, inStream.Length);