Ruby Socket Programming
Ruby provides two levels of access to network services. At the low level, you can access the operating system, which allows you to implement basic socket support for clients and servers for connection-oriented and connectionless protocols.
Ruby provides unified support for application network protocols such as FTP, HTTP, etc.
Whether high-level or low-level, Ruby provides some basic classes that let you interact with many protocols such as TCP, UDP, SOCKS, without being tied to the network layer. These classes also provide helper classes that allow you to easily read from and write to servers.
Next, let's learn how to do Ruby Socket programming.
What are Sockets
When the application layer communicates with the transport layer for data communication, TCP and UDP encounter the problem of providing concurrent services to multiple application processes at the same time. Multiple TCP connections or multiple application processes may need to transmit data through the same TCP protocol port. In order to distinguish different application processes and connections, many computer operating systems provide an interface called a socket for applications to interact with TCP/IP protocols, distinguishing network communication and connections between different application processes.
Creating a socket mainly involves three parameters: the destination IP address for communication, the transport layer protocol used (TCP or UDP), and the port number used. The original meaning of Socket is "socket". By combining these three parameters and binding them to a "socket", the application layer can use the socket interface with the transport layer to distinguish communication from different application processes or network connections, enabling concurrent data transmission services.
Socket Terms:
| Option | Description |
|---|---|
| domain | Indicates the protocol family used, usually PF_INET, PF_UNIX, PF_X25, etc. |
| type | Specifies the type of socket: SOCK_STREAM or SOCK_DGRAM. The Socket interface also defines raw sockets (SOCK_RAW), allowing programs to use lower-level protocols. |
| protocol | Usually assigned 0. |
| hostname | Identifier of the network interface:
|
| port | port is the number of the port. Each server listens on one or more port numbers for client connections. A port number can be a Fixnum port number, or a string containing the server name and port. |
Simple Client
Below we have written a simple client example using a given host and port. The Ruby TCPSocket class provides the open method to open a socket.
TCPSocket.open(hosname, port) opens a TCP connection.
Once you open a Socket connection, you can read from it like an IO object. When finished, you need to close the connection like closing a file.
The following example demonstrates how to connect to a specified host, read data from the socket, and finally close the socket:
Example
Simple Server
In Ruby, you can use the TCPServer class to write a simple service. A TCPServer object is a factory object for TCPSocket.
Now we use TCPServer.open(hostname, port) to create a TCPServer object.
Next, call the accept method of TCPServer. This method waits until a client connects to the specified port, then returns a TCPSocket object representing the connection to that client.
Example
Now run the above code on the server and see the effect.
Multi-client TCP Service
On the Internet, most services have a large number of client connections.
Ruby's Thread class can easily create multi-threaded services. One thread handles client connections, while the main thread waits for more connections.
Example
In this example, the socket runs forever. When server.accept receives a client connection, a new thread is created and immediately begins processing the request. The main program then immediately loops back and waits for new connections.
Tiny Web Browser
We can use the socket library to implement any Internet protocol. The following code shows how to retrieve the content of a web page:
Example
To implement a web-like client, you can use pre-built libraries for HTTP such as Net::HTTP.
The following code is equivalent to the previous code:Example
Above we have only briefly introduced the application of sockets in Ruby. For more documentation, please see:Ruby Socket Library and Class Methods
Other extensions