A socket is an endpoint of a two-way communication link, exposed by the operating system as an API. A connection is uniquely identified by the 5-tuple of protocol, source IP, source port, destination IP and destination port.
Why it matters
Every network program, from a browser to a database client, uses sockets. A server creates a listening socket on a port and accepts incoming connections, each of which gets its own socket. Limits on open sockets or file descriptors are a classic cause of "too many open files" errors and exhausted ports under heavy load.
Example
import socket
s = socket.create_connection(("example.com", 443), timeout=5)
print(s.getpeername())
s.close()
This opens a TCP socket to port 443 and prints the remote address. Failure raises a timeout or connection refused error.
Common mistakes
- Forgetting to close sockets, which leaks descriptors until the process fails.
- Not setting timeouts so a hung peer blocks your program forever.
- Confusing a Unix domain socket, which is local and file based, with a network socket.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.