networking · intermediate · ~15 min

Name a socket error

Map common socket errnos to names.

Challenge

Map the common socket errno values to their names — the three you meet most when connecting and binding.

Task

Implement const char *sock_err_name(int err) that returns:

  • "ECONNREFUSED" for ECONNREFUSED (nothing listening),
  • "ETIMEDOUT" for ETIMEDOUT (no route or reply),
  • "EADDRINUSE" for EADDRINUSE (port already taken),
  • "OTHER" for anything else.

Input

One int err — an errno value.

Output

Returns a pointer to the matching name string, or "OTHER".

Example

sock_err_name(ECONNREFUSED)   ->   "ECONNREFUSED"
sock_err_name(EADDRINUSE)     ->   "EADDRINUSE"
sock_err_name(EIO)            ->   "OTHER"

Input format

One integer err (an errno value).

Output format

The name string for ECONNREFUSED/ETIMEDOUT/EADDRINUSE, else "OTHER".

Constraints

Compare err against each errno constant; default to "OTHER".

Starter code

#include <errno.h>

const char *sock_err_name(int err) {
    /* TODO */
    return "OTHER";
}

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