Networking in C · advanced · ~12 min
Connect to a server and exchange bytes.
Four calls: socket(), connect(), send/recv (or read/write), close(). Build the destination address in a struct sockaddr_in (IPv4) — set family, port (htons), and address (inet_pton).
For local-only practice, use 127.0.0.1 (loopback). Never scan or connect to third-party hosts without explicit permission.
int s = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in a = {0};
a.sin_family = AF_INET; a.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &a.sin_addr);
connect(s, (struct sockaddr*)&a, sizeof a);
htons() for the port — byte order matters on the wire.