networking · intermediate · ~15 min
Map common socket errnos to names.
Map the common socket errno values to their names — the three you meet most when connecting and binding.
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.One int err — an errno value.
Returns a pointer to the matching name string, or "OTHER".
sock_err_name(ECONNREFUSED) -> "ECONNREFUSED"
sock_err_name(EADDRINUSE) -> "EADDRINUSE"
sock_err_name(EIO) -> "OTHER"
One integer err (an errno value).
The name string for ECONNREFUSED/ETIMEDOUT/EADDRINUSE, else "OTHER".
Compare err against each errno constant; default to "OTHER".
#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.