networking · beginner · ~15 min

Reliable transport?

Map socket type to reliability.

Challenge

TCP (SOCK_STREAM) provides reliable, ordered delivery; UDP (SOCK_DGRAM) does not. Map a socket type to whether it is reliable.

Task

Implement int is_reliable(int socktype) that returns 1 for SOCK_STREAM, otherwise 0.

Input

One int socktype (e.g. SOCK_STREAM, SOCK_DGRAM).

Output

Returns 1 for SOCK_STREAM, else 0.

Example

is_reliable(SOCK_STREAM)   ->   1
is_reliable(SOCK_DGRAM)    ->   0

Input format

One socket-type constant socktype.

Output format

1 if socktype is SOCK_STREAM, else 0.

Constraints

SOCK_STREAM (TCP) is reliable; SOCK_DGRAM (UDP) is not.

Starter code

#include <sys/socket.h>

int is_reliable(int socktype) {
    /* TODO */
    return 0;
}

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