Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6644527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:10:41+00:00 2026-05-26T00:10:41+00:00

Hello guys im getting This error please see the attachment >> Index and count

  • 0

Hello guys im getting This error please see the attachment >> Index and count must refer to a location within the buffer. Parameter name: bytes

enter image description here

When im using debugger i dont get this error and everything goes fine i cannot understand what this error is

this is my server code :

 IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 27015);
            Socket sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            sck.Bind(ipEnd);
            sck.Listen(100);

            Socket clientSocket = sck.Accept();

            string[] fNames = new string[3];
            fNames[0] = "01.jpg";
            fNames[1] = "02.jpg";
            fNames[2] = "03.jpg";

            string filePath = "D:\\";

            byte[] FilesCount = BitConverter.GetBytes(fNames.Count());


            clientSocket.Send(FilesCount);

            for (int i = 0; i < fNames.Count(); i++)
            {
                byte[] fileNameByte = Encoding.ASCII.GetBytes(fNames[i]);
                byte[] fileData = File.ReadAllBytes(filePath + fNames[i]);
                byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
                byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

                fileNameLen.CopyTo(clientData, 0);
                fileNameByte.CopyTo(clientData, 4);
                fileData.CopyTo(clientData, 4 + fileNameByte.Length);

                clientSocket.Send(clientData);
            }

            clientSocket.Close();

And Client:

   Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
        clientSock.Connect(IPAddress.Parse("46.49.70.30"), 27015);


        byte[] clientData = new byte[1024 * bt];
        string receivedPath = "D:/df/";

        byte[] FileQuantityByte = new byte[1024];
        clientSock.Receive(FileQuantityByte);
        int FileQuantity = BitConverter.ToInt32(FileQuantityByte, 0);

        for (int i = 0; i < FileQuantity; i++)
        {
            int receivedBytesLen = clientSock.Receive(clientData);

            int fileNameLen = BitConverter.ToInt32(clientData, 0);
            string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

            //Console.WriteLine("Client:{0} connected & File {1} started received.", clientSock.RemoteEndPoint, fileName);

            BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append));
            bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

            //Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath);

            bWrite.Close();
        }

        clientSock.Close();

EDIT : http://imageshack.us/f/202/errbk.jpg/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:10:41+00:00Added an answer on May 26, 2026 at 12:10 am

    The exception is telling exactly what the problem is: one of your parameters isn’t what you think it should be.

    What is the length of clientData? What is the value of fileNameLen when you call Encoding.ASCII.GetString? What is the value of bt, which is used to initialize your clientData array?

    If this doesn’t happen in the debugger, then add some code to output the values of clientData and fileNameLen before the call.

    One problem is that clientSock.Receive might not get all of the data at once. If you’re sending an especially large file, it’s possible that clientSock.Receive will return without reading everything. As documentation for Socket.Receive says:

    If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer.

    It’s possible that not all of the data is available yet, or that the buffer is smaller than the file size. To ensure that you get all of the data, you have to do this:

    int totalBytesRead = 0;
    int bytesRead = 0;
    while ((bytesRead = clientSock.Receive(clientData, totalBytesRead,
        clientData.Length - totalBytesRead, SocketFlags.None)) != 0)
    {
        totalBytesRead += bytesRead;
    }
    

    Receive will return 0 when there is no more data available. Only then can you be sure that you’ve received all of the data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello guys I have a little trouble with the my method [HTTPost], please see
Hello can anybody solve this please I'm creating the object in the action class
Hello guys on my blog I use this script: jQuery(document).ready(function () { jQuery(.postbox).hover(function ()
hello guys i am doing some database operations in iphone .. please can anyone
Hello guys i have a problem streaming PDF files with php, i'm using this
hello guys i have this function for focus : fav[jj].setOnFocusChangeListener(new View.OnFocusChangeListener() { public int
Hello guys, I have an error I cannot solve, on a ASP.NET website. One
hello guys im using this second part of pagination script to show pagination; <?
Possible Duplicate: string split in c# Hello guys i am getting connected ip address
Hello guys i am new about this implementing facebook into my app.I check developer's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.