Ruby Socket Programming
Ruby provides two levels of access to the network, and at the bottom you can access the operating system, which allows you to implement basic socket support for both client and server for connection-oriented and connectionless protocols.
Ruby unified support for application network protocols such as FTP, HTTP, and more.
Whether it is high-level or low-level. Ruby provides some basic classes that allow you to interact with many protocols, such as TCP, UDP, SOCKS, etc., without having to stick to the network layer. These classes also provide helper classes that allow you to easily read and write to the server.
Next let's learn how to do Ruby Socket programming
What is Sockets
When the application layer communicates data through the transport layer, TCP and UDP encounter the problem of providing concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may need to transfer data over the same TCP protocol port. To distinguish between different application processes and connections, many computer operating systems provide an interface called Sockets for applications to interact with the TCP/IP protocol, distinguishing between network communications and connections between different application processes.
The generated socket has three main parameters: the destination IP address of the 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 to a "socket" socket, the application layer can communicate with the transport layer through a socket interface to distinguish communication from different application processes or network connections, and implement concurrent services for data transmission.
Sockets vocabulary analysis:
|domain||indicates which protocol family to use, usually PF_INET, PF_UNIX, PF_X25, and so on.|
|type||Specify the type of socket: SOCK_STREAM or SOCK_DGRAM. The Socket interface also defines the original Socket (SOCK_RAW), allowing the program to use low-level protocols|
|protocol||is usually assigned a value of 0.|
|hostname||Network interface identifier:
|port||port is the port number, each server will listen to one or more port numbers of the client connection, a port number can be the port number of Fixnum, including the server name and port.|
The following we have written a simple client instance for a given host and port. The Ruby TCPSocket class provides an open method to open a socket.
TCPSocket.open(hosname, port ) Open a TCP connection.
Once you open a Socket connection, you can read it like an IO object. When you're done, you need to close the connection as if you were closing the file.
The following example demonstrates how to connect to a specific host, read data from the socket, and finally close the socket:
The TCPServer class can be utilized in Ruby to compose a straightforward administration. The TCPServer object is a processing plant object for TCPSocket.
Now we use TCPServer.open(hostname, port) to make a TCPServer object.
Next call TCPServer's acknowledge strategy, which trusts that a customer will associate with the predetermined port and afterward restores a TCPSocket object showing that it is associated with the customer.
Now, run the above code on the server to see the impact.
Multi-customer TCP service
On the Internet, most administrations have an expansive number of customer associations.
Ruby's Thread class makes it simple to make multithreaded administrations, one string performs customer associations, and the principle string is hanging tight for more associations.
In this model, the attachment is running forever, and when server.accept gets the customer's association, another string is made and the solicitation is prepared right away. The primary program circles back quickly and hangs tight for another association.
Small web browser
We can utilize the attachment library to actualize any Internet convention. The accompanying code tells the best way to get the substance of a webpage:
To actualize a web-like customer, you can utilize a library pre-worked for HTTP, for example, Net::HTTP.The following code is equal to the past code:
The above is only a short prologue to the utilization of attachments in Ruby. For more documentation, if you don't mind see: Ruby Socket library and class methods