In this article, we’re going to explore the following topics:
Along the time, communication over computers has been essential to the evolution of computer science in the world.
Indeed, this allowed us to:
- exchange information (chat, mail, etc..)
- share files (FTP, etc..)
- spread an idea, an opinion, a message (Internet, ..)
- create new business opportunities
In this article, we’re going to see how Ruby implemented network programming in its Core and Standard libraries.
The socket library
First, and if you’re not familiar with what’s a socket:
Sockets are endpoints of a bidirectional communication channel.
Sockets can communicate within a process, between processes on the same machine or between different machines.
socket library is in charge of providing an access to the different kind of existing sockets:
- TCP socket — via the
- UDP socket — via the
- UNIX socket — via the
This library is part of the Ruby Standard Library.
In Ruby, the root class of any
Socket is the
This class inherits from
IO class is in charge of handling Input and Output stream — like an instance of
This means that our
socket is interpreted as an I/O stream.
BasicSocket class provides a bunch of methods to access the properties of a
socket and few methods to read and write through the socket — such as blocking and non-blocking
UNIXSocket classes derivate from
UDPSocket derivates from
IPSocket which derivates from
They all use the routines defined in
BasicSocket and implement the protocol associated to their socket type:
socket library is in charge of handling low-level communications via the Internet Protocol (UDP and TCP) and the Inter-Process Communication mechanism.
Now, let’s see how Ruby implements high-level protocols such-as
net library is in charge of providing an implementation of high-level protocols.
The supported protocols are
We can see that all of these protocols are implemented in a specific class scoped in the
Also, these classes derivate from the
Let’s have a look to the content of this class
Net::Protocol class inherits from
Object — the default inherited class in Ruby.
Feel free to read my article about the
Ruby Object Modelif you’re unfamiliar with the
Objectclass in Ruby.
It also implements a
protocol_param class method which is used to define the expected protocol parameters.
It’s a very light object.
Otherwise, requiring the
net/protocol library adds a bit more than this root class.
Feel free to read the
Requiring a file or library in Rubyif you’re not familiar with the
Kernel#requiremethod in Ruby.
Indeed, in addition of the
Net::Protocol class, the
- adds a set of error classes.
- requires other libraries needed by protocols — such as the
- adds a set of classes to control read/write operations through sockets—
So, a class that derivates from
Net::Protocol just provides an implementation of a given protocol and handles errors and I/O stream operations by using the tools provided by the
As Ruby is a fully object-oriented programming language, the implementation of network programming takes advantage of all the tools provided by this languages — such as namespacing, inheritance, root class, error handling, feature inclusion (
Also Ruby implements low-level and high-level network protocols in two different libraries: the
Finally, you can easily implement a new protocol by enjoying the abstraction provided by Ruby — by inheriting of
Net::Protocol depending on your needs.
ONE MORE THING ⬇
Feel free to subscribe here: www.rubycademy.com
Thank you for taking the time to read this post :-)
Feel free to 👏 and share this article if it has been useful for you. 🚀
Also, as I post an article every 3 days, feel free to follow me to be notified of my new article releases.
Here is a link to my last article: