networking · beginner · ~15 min

Is it a stream socket?

Recognise the stream socket type constant.

Challenge

Recognise the TCP (stream) socket-type constant.

Task

Implement int is_stream_socket(int type).

Input

  • type: a socket-type constant (e.g. SOCK_STREAM, SOCK_DGRAM).

Output

Return 1 if type is SOCK_STREAM (TCP), else 0.

Example

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

Input format

type: a socket-type constant.

Output format

1 if type is SOCK_STREAM, else 0.

Constraints

Compare against SOCK_STREAM.

Starter code

#include <sys/socket.h>

int is_stream_socket(int type) {
    /* TODO */
    return 0;
}

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