This post will cover a little bit about the abstraction of a Socket in Linuxworld, what they exactly are and how they are generally used.
What are Sockets and how are they created?
Sockets in the most general sense are an abstraction of interprocess communication. They represent the endpoint of a communication channel (abstracting the details of the underlying transport).
To interact with Sockets, you use system calls. Those are commands that you send the Linux kernel who manages the communication and networking stack.
Sockets are created by using the
socket() system call.
Let’s deconstruct this call by looking at it’s arguments.
int socket(int domain, int type, int protocol)
The communication domain specifies a family of protocols that this Socket is going to live in. Most prominently, Sockets can be “scoped” to the local machine (Unix socket) or the internet.
- UNIX domain (identified by the
- IPv4 internet (
- IPv6 internet (
- a bunch more, which can be found in the man page
The Socket type specifies the communication semantics which are going to be in effect for this socket. Let’s go through the most common examples:
Stream sockets (
Stream sockets provide what is called a sequenced, reliable, two-way, connection-based byte stream. Deconstructing this in terms of guarantees provided:
- Sequenced: Messages are received in the same order in which they were sent
- No duplication: The same message is received at most once
- No creation: Messages are not received unless sent (there are no random messages appearing out of thin air)
- Validity: When a process sends a message to another one (given that they’re correct and not buggy), it will eventually be received
- two-way: Both sides are able to send and receive
- connection-based: A session is established prior to data transfer
The protocol in the Internet communication domain for this socket type is TCP.
Datagram sockets (
A datagram is:
A self-contained, independent entity of data carrying sufficient information to be routed from the source to the destination computer without reliance on earlier exchanges between this source and destination computer and the transporting network. RFC1594
Datagram sockets provide sending of connectionless, unreliable messages of a fixed maximum length. The protocol in the Internet communication domain for this socket type is UDP.
There are more types of sockets, which can be found in the man page
In most cases, the
int protocol is redundant, it is used when the communication domain has more than one protocol available for use (for example on
SOCK_RAW type sockets). Otherwise it can be nulled and ignored.
Obviously, Sockets are only useful when there is another program on the other end of the communication channel. So how do Sockets connect to each other?
Most commonly, this happens by a Socket connecting to another Socket that is listening. Let’s step through this by a TCP client-server example.
When a Server is starting up, it:
- Instantiates a Socket through the
socket()system call that we just saw.
- Binds it to a name (in this case IP address and port) through
- Allows incoming connections on the Socket by calling
- The socket is now able to be connected to, essentially putting incoming connections into a queue
accept()pulls connections from the queue and allocates a new Socket for them which is returned
When trying to establish a connection to a Server, the steps are:
- Instantiating a Socket through
socket(), just like in the Server
connect()with the name / address that the Server socket was bound to (in
SOCK_STREAMsockets this attempts to initialize the session with the Server)
- Sending some data (e.g., the HTTP request) to the server by using
write()(which behaves the same, but you cannot set flags)
- Reading a response using
read()- this call blocks (does not return) waiting unless the socket is set to use non-blocking I/O (by using
- When done interacting with the Server,
close()is called on the Socket, which deletes it and closes the established connection.
Hope this has taught you a thing or two about Sockets and given you a bit of insight on how Sockets actually work close to the metal.
Until next time!