Socket Programming Basics·Lesson 2 of 9
What is a Socket?
A Socket Is an Endpoint
A socket is one end of a two-way communication link between two programs running over a network. Think of a connection as a pipe between two programs: a socket is the opening of that pipe on one side. Your program writes data into its socket to send it, and reads data out of its socket to receive it. Each side — sender and receiver — has its own socket.
The plain definition
A socket is a communication endpoint. One program's socket connects to another program's socket, and data flows between them.
Like an electrical socket
A wall socket is the point where a device plugs into the electrical system. A network socket is where your program plugs into the network. Once plugged in, data can flow in and out — just as power flows once an appliance is connected.
A Socket Is Software, Not Hardware
A socket isn't a physical connector you can touch. It's a software abstraction created and managed by your computer's — the master program (like Windows, macOS, or Linux) that controls the machine and its hardware. When your program asks for a socket, the operating system hands back a handle it can use to send and receive, and quietly takes care of the actual network hardware for you.
It helps to picture where a socket sits. Your code is at the top; the raw network is at the bottom; the socket is the doorway in between, offered by the operating system so your program never has to deal with wires and signals directly:
Where a socket sits
Your application
The program you write — sends and receives through the socket
Socket API
The set of functions the OS gives you to use a socket
OS network stack
The OS handles TCP/UDP and IP, packaging your data into packets
Physical network
Cables, Wi-Fi, and routers that actually carry the bits
The Socket API
To use a socket, your program calls functions provided by the operating system. This set of functions is called the . Different languages wrap it in their own style, but the core operations are almost always the same handful. In rough order of use:
Core socket operations
- create — ask the OS for a new socket
- bind — attach the socket to a local address and port (mainly on servers)
- connect — reach out to another socket's address (on the client)
- listen and accept — wait for and take incoming connections (on the server)
- send and receive — write data out and read data in
- close — shut the socket down when you're done
Like reading and writing a file
Once a socket is open, sending and receiving feel a lot like writing to and reading from a file — except the data travels across the network to another program instead of to disk.
A Socket Has an Address
For two sockets to find each other, each needs an address. A socket's address is made of two parts: an IP (Internet Protocol) address that says which machine, and a port number that says which program on that machine. We unpack both in the next lesson, but keep the pairing in mind — it's what makes a socket reachable.
“"A socket is a physical port or connector on the computer."”
Q:What is a socket?
A: A socket is a communication endpoint — one end of a link between two programs over a network. It's a software abstraction provided by the operating system, identified by an IP address and a port, that a program uses to send and receive data.
Quick Revision Cheat Sheet
Socket: A communication endpoint between two programs
Nature: Software abstraction from the OS, not hardware
Socket API: OS functions: create, bind, connect, listen, accept, send, recv, close
Address: An IP address plus a port number
Feels like: Reading and writing a file, but over the network