networking · beginner · ~15 min
Check whether a recv got the full amount.
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.
Implement int recv_complete(int got, int want) that returns 1 if got == want, otherwise 0.
got: bytes received so far.want: bytes expected.Returns 1 if got == want, else 0.
recv_complete(100, 100) -> 1
recv_complete(60, 100) -> 0
Two integers: bytes received (got) and bytes wanted (want).
1 if got == want, else 0.
Completion is an exact equality between got and want.
int recv_complete(int got, int want) {
/* TODO */
return 0;
}
Assuming read/write transmits all requested bytes at once; ignoring socket file descriptor cleanup; unhandled endianness conversions.
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.