SOCKET LIBRARY IN RUBY ON RAILS

There are basically two levels of accessing network services in case of rails .

The socket and network libraries are such important parts of integrating Ruby applications with the Internet and other communications-based environments.

One is through socket libraries provide low level access to connection oriented and connection-less protocols. Where user can implement client and server connection.

Ruby is also having Network libraries that provide users a high level access to specific network protocols like FTP,HTTP and Etc.

At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols.

Ruby also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.

What are Sockets?

Sockets are basically a bidirectional communications channel. Sockets may communicate within a process, between processes on the same machine, or between processes on different continents.

Sockets can be implemented on different channels : Unix domain sockets, TCP, UDP, and so on. The socket library mainly provides specific classes and interface for handling a transports.

A Simple Client:

Hereis a very basic client program which will open up a connection to a given port and given host. Ruby class TCPSocket provides open function to open a socket.

The TCPSocket.open(hosname, port ) opens a TCP connection to hostname & theport.

Once socket is open, you can actually read that particular thing like IO object. When it’s done, just remember to close it, as you close that file.

The following code connects to a given host and port, reads any available data from the socket, and then exits:

require 'socket'      # Socket in standard library
hostname = 'localhost'
port = 8080
s = TCPSocket.open(hostname, port)
while line = s.gets   # Gets and Read the  lines from the socket
puts line.chop # And print with platform line terminator
end
s.close # socket Closed

A Simple Server:

To write Internet servers, we basically use this TCPServer class. A TCPServer object is a factory for TCPSocket objects.

Now call TCPServer.open(hostname, port function to specify a port for your service and create a TCPServer object.

Next, call the accept method of the returned TCPServer object. This particular method waits till client connects to the port you specified above, and then returns a TCPSocket object that basically represents the connection to that client.

require 'socket'               # Get sockets from stdlib
server = TCPServer.open(8080)  # Socket to listen on port 8080
loop { # Servers run forever
client = server.accept # Wait for a client to connect
client.puts(Time.now.ctime) # Send the time to the client
client.puts "Closing the connection. Bye!"
client.close # Disconnect from the client
}

Now run this server in background and then run above client to see the result.