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

  • SEARCH
  • Home
  • 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 166937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:11:08+00:00 2026-05-11T12:11:08+00:00

Following my previous question ( Why do I get weird results when reading an

  • 0

Following my previous question (Why do I get weird results when reading an array of integers from a TCP socket?), I have come up with the following code, which seems to work, sort of. The code sample works well with a small number of array elements, but once it becomes large, the data is corrupt toward the end.

This is the code to send the array of int over TCP:

#define ARRAY_LEN 262144  long *sourceArrayPointer = getSourceArray();  long sourceArray[ARRAY_LEN]; for (int i = 0; i < ARRAY_LEN; i++) {     sourceArray[i] = sourceArrayPointer[i]; }  int result = send(clientSocketFD, sourceArray, sizeof(long) * ARRAY_LEN); 

And this is the code to receive the array of int:

#define ARRAY_LEN 262144  long targetArray[ARRAY_LEN]; int result = read(socketFD, targetArray, sizeof(long) * ARRAY_LEN); 

The first few numbers are fine, but further down the array the numbers start going completely different. At the end, when the numbers should look like this:

0 0 0 0 0 0 0 0 0 0 

But they actually come out as this?

4310701 0 -12288 32767 -1 -1 10 0 -12288 32767 

Is this because I’m using the wrong send/recieve size?

  • 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. 2026-05-11T12:11:09+00:00Added an answer on May 11, 2026 at 12:11 pm

    The call to read(..., len) doesn’t read len bytes from the socket, it reads a maximum of len bytes. Your array is rather big and it will be split over many TCP/IP packets, so your call to read probably returns just a part of the array while the rest is still ‘in transit’. read() returns how many bytes it received, so you should call it again until you received everything you want. You could do something like this:

    long targetArray[ARRAY_LEN];  char *buffer = (char*)targetArray; size_t remaining = sizeof(long) * ARRAY_LEN; while (remaining) {   ssize_t recvd = read(socketFD, buffer, remaining);   // TODO: check for read errors etc here...   remaining -= recvd;   buffer += recvd; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from my previous question , I have now managed to rotate my
Following the answer from a previous question I want to get rid of some
From a previous question , I have the following: So I have implemented a
Following on from a previous question about sub-selects, I have an SQL statement with
Following on from my previous question I have been working on getting my object
Ok, so following on from my previous question I have ended up with the
Following up from my previous question , why is CShell so different from C?
Following on from a previous question, for some reason when I use the following
Following on from a previous question relating to heap usage restrictions , I'm looking
Following on from a previous question , I am creating a symbolic link on

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.