networking · beginner · ~10 min · safe pentest lab

Connect from a TCP client to a listener

Build a sockaddr_in and call connect() on it.

Challenge

Implement int connect_local(int port) that opens a TCP socket and connects to 127.0.0.1:port. Returns the connected fd, or -1 on any failure.

Why this matters

The full client prologue: socket → fill sockaddr_in → connect.

Starter code

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int connect_local(int port) {
    /* TODO */
    return -1;
}

Background lessons

Up next

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