Search code examples
c#winformssocketsclient-servertcp

System.Argument out of exception:Specified argument was out of range of valid value


After several fixes, I am still having a nasty error. I debug and run my server using Developer Command Prompt for VS2013 and run client using VS 2013.

Firstly I run my server without any issues, but when I start the client, put data in a textbox there and click send problem arises.

This is the code on server side which directs the issue.

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();

EDIT : After a suggestion, I changed :

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

to

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

resulted in the screen shot given but with no data from client received.

Screenshot

Error (from Command Prompt):-

Parameter name:size at System.Net.Sockets.NetworkStream.Read(byte[] buffer,int offset,int size)

at Console.Application1.Prog.Main():
System Argument Out of Exception:Specified Argument was out of range of valid value.

What actually are they calling valid value.

PS. Apology if the code here I provided is too long but its the need


Solution

  • You should specify receiveBuffer size as third parameter, not clientSocket.ReceiveBufferSize

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

    OR

    create a buffer with clientSocket.ReceiveBufferSize;

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

    Here is example from 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();
          }