networking · beginner · ~15 min

Did we receive everything?

Check whether a recv got the full amount.

Challenge

recv() can return fewer bytes than requested, so you loop until you have them all. Check whether the running total of received bytes has reached the expected amount.

Task

Implement int recv_complete(int got, int want) that returns 1 if got == want, otherwise 0.

Input

  • got: bytes received so far.
  • want: bytes expected.

Output

Returns 1 if got == want, else 0.

Example

recv_complete(100, 100)   ->   1
recv_complete(60, 100)    ->   0

Input format

Two integers: bytes received (got) and bytes wanted (want).

Output format

1 if got == want, else 0.

Constraints

Completion is an exact equality between got and want.

Starter code

int recv_complete(int got, int want) {
    /* TODO */
    return 0;
}

Common mistakes

Assuming read/write transmits all requested bytes at once; ignoring socket file descriptor cleanup; unhandled endianness conversions.

Edge cases to handle

Short reads/writes; peer closing socket (recv returns 0); signal interruption (EINTR); non-blocking EAGAIN.

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