Networking in C · advanced · ~10 min

Localhost port checking

Detect which loopback ports accept TCP connections.

Lesson

A "scan" simply tries to connect() to each port. A successful connect means something is listening; a refused connect (ECONNREFUSED) means nothing is there.

Strictly local. Scanning third-party hosts without explicit, written authorisation is unauthorised access in many jurisdictions. The platform's exercises only ever target 127.0.0.1 against fixtures the harness opens itself.

Common mistakes

  • Reusing the same socket fd for each attempt — open a new socket per port.