DevPrep

All topics › Networking & APIs

Socket

Middle PRO

Essence

A socket is a programming interface and an abstraction for a connection's "endpoint". A socket is defined by the triple protocol + IP address + port: e.g. TCP, 2001:db8::1, 443. The address selects the machine (IPv4 — 32 bits, IPv6 — 128 bits; Apple requires IPv6-only network support), the port (0–65535) selects a specific application on it. A connection is a pair of sockets: client and server.

The server creates a listening socket, binds it to a port (bind) and waits for incoming connections (listen/accept); the client calls connect. Then both sides read and write bytes (send/recv). These are BSD sockets — the POSIX C API.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store