networking · beginner · ~15 min

Did we receive the whole message?

Verify the assembled message length.

Challenge

After a recv loop, you have a list of how many bytes each recv returned. Confirm you reassembled the full message by summing those counts and comparing to the expected length.

Task

Implement int all_received(const int *per_read, int n, int expected) that returns 1 if the sum of the first n entries of per_read equals expected, otherwise 0.

Input

  • per_read: the per-recv byte counts.
  • n: number of entries.
  • expected: the expected total length.

Output

Returns 1 if the counts sum to expected, else 0.

Example

all_received({256,256,256,232}, 4, 1000)   ->   1
all_received({100,100}, 2, 300)            ->   0

Input format

An int array per_read of per-recv counts, its length n, and the expected total.

Output format

1 if the per_read counts sum to expected, else 0.

Constraints

Sum per_read[0..n-1] and compare to expected.

Starter code

int all_received(const int *per_read, int n, int expected) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.