Snapcat

How network programming is represented in Ruby

In this article, we’re going to explore the following topics:

  • the socket library
  • the net library

Introduction

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.

The socket library is in charge of providing an access to the different kind of existing sockets:

  • TCP socket — via the TCPSocket class
  • UDP socket — via the UDPSocket class
  • UNIX socket — via the UnixSocket class
  • etc..

This library is part of the Ruby Standard Library.

In Ruby, the root class of any Socket is the BasicSocket class

This class inherits from IO.

The IO class is in charge of handling Input and Output stream — like an instance of File, $stdin , $stdout or $stderr.

This means that our socket is interpreted as an I/O stream.

Also, the 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 read and write.

TCPSocket, UDPSocket & UNIXSocket classes derivate from BasicSocket.

Actually, TCPSocket & UDPSocket derivates from IPSocket which derivates from BasicSocket.

They all use the routines defined in BasicSocket and implement the protocol associated to their socket type:

  • the TCP protocol for TCPSocket
  • the UDP protocol for UDPSocket
  • the IPC mechanism for UNIXSocket

The 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 HTTP, IMAP, SMTP and FTP.

The net library

The net library is in charge of providing an implementation of high-level protocols.

The supported protocols are FTP, HTTP, IMAP, POP andSMTP

We can see that all of these protocols are implemented in a specific class scoped in the Net namespace.

Also, these classes derivate from the Net::Protocol class.

Let’s have a look to the content of this class

The Net::Protocol class inherits from Object — the default inherited class in Ruby.

Feel free to read my article about the Ruby Object Model if you’re unfamiliar with the Object class 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 Ruby if you’re not familiar with the Kernel#require method in Ruby.

Indeed, in addition of the Net::Protocol class, the net/protocol library:

  • adds a set of error classes.
  • requires other libraries needed by protocols — such as the socket library.
  • adds a set of classes to control read/write operations through sockets— BufferedIO, WriteAdapter, etc..

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 net/protocol library.

Conclusion

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 (require keyword).

Also Ruby implements low-level and high-level network protocols in two different libraries: the socket and net/* libraries.

Finally, you can easily implement a new protocol by enjoying the abstraction provided by Ruby — by inheriting of BasicSocket or Net::Protocol depending on your needs.

Voilà!

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:

Inheritance on-the-fly with Ruby on Rails