networking · beginner · ~15 min
Bound a listen() backlog argument.
The listen() backlog argument should be kept within a sensible range. Clamp a requested backlog so it never goes below 0 or above a maximum.
Implement int clamp_backlog(int requested, int max) that returns requested clamped to the range [0, max].
requested: the requested backlog (may be negative).max: the upper cap.Returns requested if it is within [0, max], otherwise the nearest bound.
clamp_backlog(50, 128) -> 50
clamp_backlog(500, 128) -> 128
clamp_backlog(-1, 128) -> 0
requested clamps up to 0.requested above max clamps down to max.Two integers: the requested backlog and the maximum.
requested clamped into the range [0, max].
Floor the result at 0 and cap it at max.
int clamp_backlog(int requested, int max) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.