Home » Ruby programming

Socket programming in Ruby

Ruby socket programming: In this tutorial, we are going to learn about the socket programming with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on September 07, 2019

Ruby socket programming

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).

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.

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.

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:

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

skt = TCPSocket.open(hostname, port)   

while ln = skt.gets   
	puts ln.chomp   
end   

skt.close  

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.

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.