×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Socket programming in Ruby

By IncludeHelp Last updated : December 03, 2024

Purpose of Sockets

The main purpose of the socket is to allow the client and server to communicate with each other while socket programming is a mechanism to create a communication between two nodes (sockets).

Levels of Network Access

Two levels are allowed in Ruby to access network services. Basic Socket support is permitted in level one which allows you to make use of client and server under both connectionless and connection-oriented protocols (support by your Operating System) whereas level two provides you access to the predefined libraries which helps you to access application-level network protocols namely HTTP, FTP and so on.

Example of Socket Programming

Let us understand how socket programming is done in Ruby with the help of an example given below:

require 'socket'   
server = TCPServer.open(2000)   

loop {   
	client = server.accept   
	client.puts "Hello. Greetings from Includehelp.com"  
	client.puts "Hope, you all are enjoying the day"
	client.puts "This is an example of socket programming."
	client.close   
}

The above program is a simple client program which will open a connection with the provided port address. We are utilizing a loop under which we are taking an instance client. The client is accepting all the connections made to port 2000 and sending data to the client using the Socket network. The data sent here are strings. At last, we are closing the socket.

Fetching Data from the Server Using a Socket

We saw how to send data over a socket? Now, let us see how to fetch data from the server over the socket. Refer the code given below:

Example

require 'socket'   
hostname = 'localhost'   
port = 2000  

skt = TCPSocket.open(hostname, port)   

while ln = skt.gets   
	puts ln.chomp   
end   

skt.close  

Explanation

In the above code, we are including a pre-installed module named Socket. We are then assigning values to localhost and port. After that, we are using 'skt' to open TCPSocket along with hostname and port. Use while loop to fetch all the data sent over the network. In the end, close the socket.

Handling Multiple Clients with a Single Server

The codes we read above allow one client and one server at a time. But we may need a code which allows multiple clients to get connected with a single server at a time. In such a case, the following code may provide some help:

require 'socket'                
server = TCPServer.open(2009)    

loop {                          
	Thread.start(server.accept) do |client|
	client.puts "Hey Client! you are connected with the server."
	client.puts "Sending...........................!"
	client.puts "Its time to close the connection. See you super soon!"
	client.close                  
	end
}

Thread class defined in Ruby library helps in creating a multithreaded network. Thread class a new execution thread to process the connection instantly while permitting the main class to anticipate more connections. In the above program, we are making use of Thread class to connect with multiple clients.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.