networking · beginner · ~15 min

Valid backlog value?

Validate the listen backlog argument.

Challenge

Validate the backlog argument to listen(), which bounds the pending-connection queue.

Task

Implement int valid_backlog(int b).

Input

  • b: the proposed backlog value.

Output

Return 1 if b >= 0 (a valid backlog), else 0.

Example

valid_backlog(128)  ->  1
valid_backlog(0)    ->  1
valid_backlog(-1)   ->  0

Input format

b: the proposed listen backlog.

Output format

1 if b >= 0, else 0.

Constraints

The backlog must be non-negative.

Starter code

int valid_backlog(int b) {
    /* 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.