I keep seeing sockets mentioned in networking explanations. What exactly is a socket, and how does a program use one?
What is a socket, in technical but understandable terms?
Answers
A socket is a communication endpoint that a program asks the operating system to create. The program can use it to send and receive data without managing all the low-level network details itself.
For example, a web server can listen on a socket for incoming connections. A browser connects to it, and the two programs use their respective sockets to exchange requests and responses. The operating system handles the networking underneath.
Reply to this answer
On Unix-like systems, a socket often feels a bit like an open file. A program gets a file descriptor and can read or write data through it. That does not mean the socket is a regular file containing stored data, though. The operating system passes the data between communication endpoints.
Some sockets connect programs on the same machine rather than across a network. A Unix domain socket can have a filesystem pathname that other programs use to find it, but that pathname is not a file full of messages.
Reply to this answer
The type of socket matters. TCP sockets provide a connected stream of bytes. UDP sockets send and receive separate datagrams, without establishing a TCP-style connection. An IP address and port help identify network endpoints, but they are not the socket object itself.
Reply to this answer
Add your answer
Answers are reviewed before publication.
p.804~clamptray:
So the socket is an endpoint, not the connection itself. Each side of a connection has its own socket.