Ruby is a general-purpose language used to create web applications, as it has development frameworks already installed.
With Ruby, we can implement client-server models to send requests through
The Ruby language supports libraries that give high-level access to application-level specified networking protocols, such as File Transfer Protocol (FTP), HyperText Transfer Protocol (HTTP), Transmission Control Protocol (TCP), and more.
TCPServer
classIf we want to design a server connection, we’ll have to use the TCPServer
class in Ruby.
The TCPServer
class supports multiple attributes necessary for building a server-client relationship in Ruby. A TCPServer
class object works as a factory for sockets.
To run a Ruby server program, we have to integrate the TCPServer
class.
Since we have to connect to a server, we need a hostname
, port
, and socket
package to include some commands.
Let’s implement a server request using the TCPServer
class.
require 'socket' server = TCPServer.new('localhost', 3306) loop do request = server.accept request.puts 'simple server request in ruby' request.close end
The above program demonstrates a simple server connection. Line 1 (require 'socket'
) includes the libraries and attributes necessary for a TCPServer
connection.
The TCPServer.new(...)
accepts two parameters in this case: localhost
and a port number 3306
. This method creates a new server.
The body of loop do-end
opens a server request, sends a message ‘simple server request in ruby,’ and closes the server request using request.close
.
RELATED TAGS
CONTRIBUTOR
View all Courses