Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Ruby Programming Language Enables Concise Network Programming

Reduce development time by using Ruby to access network services. Not only is Ruby a high-performance programming language, but its concise programming notation also enables you to write programs quickly.


advertisement
ometimes runtime performance requirements determine which programming language I use. For flat out runtime performance, I have always found that natively compiled Common Lisp or C++ really do the trick. For large-scale Web applications, I usually use Java because of its wealth of infrastructure software.

For other projects, I find myself using the Ruby programming language more and more. Not only is Ruby a high-performance programming language that minimizes software development and maintenance costs, but writing software in it is just fun! Ruby is a very concise programming notation, so you can write programs quickly. The readability of Ruby code also makes it easy to make changes later.

I currently use Ruby for small text-based utility programs and network programming, and I use Ruby on Rails for some Web application development. This article concentrates on using Ruby for network programming and provides examples for Web service implementations with Ruby. Don't worry if you're new to Ruby. You really don't need to know much about it in order to get something from this tutorial. The Related Resources section in the left column offers links to other Ruby tutorials and books for further reading.



Simple Server-Side Ruby: a Web Server

The Ruby language is supported by a rich built-in library. This first example uses the TCPServer class to open a server socket on port 9090 and waits for incoming connections. The goal is to be able to handle HTML GET requests like http://127.0.0.1:9090/foo?bar=123.

The following Ruby code opens a server TCP socket, accepts incoming session requests, and writes each incoming request back to the user's browser:

Listing 1
require 'socket' server = TCPServer.new('127.0.0.1', 9090) while (session = server.accept) request = session.gets puts request session.print "HTTP/1.1 200/OK\rContent-type: text/html\r\n\r\n" session.print "<html><head><title>Response from Ruby Web server</title></head>\r\n" session.print "<body>request was:" session.print request session.print "</body></html>" session.close end
The line "puts request" prints out incoming browser requests:

GET /foo?bar=123 HTTP/1.1 GET /favicon.ico HTTP/1.1

The second line printed because my browser (Safari on OS X) requested a "favorite icon" for this Web site. Fetching this response also would be simple without using a Web browser but instead using some simple Ruby TCP client code (here I use the "open-uri" library, which allows a remote resource identified by a URI to be accessed like a local file):

Listing 2
require 'open-uri' # allows the use of a file like API for URLs open("http://127.0.0.1:9090/foo?bar=123") { |file| lines = file.read puts lines }
The output would be:

<html><head><title>Response from a very simple Ruby Web server</title></head> <body>request was:GET /foo?bar=123 HTTP/1.1 </body></html>

While extending this simple Web server to return local HTML files, etc. would be trivial, it wouldn't make much sense: the standard Ruby library contains the flexible WEBrick Web server toolkit. If you want to set up a full-featured Web site using pure Ruby, use WEBrick! Using the standard library, you can create a full service Web server with just a few lines of code (from the WEBrick documentation):

Listing 3
require 'webrick' include WEBrick s = HTTPServer.new(:Port => 9090, :DocumentRoot => Dir::pwd + "/htdocs") trap("INT"){ s.shutdown } s.start
Sometimes writing a simple Web server from scratch in Ruby is useful. Instead of returning HTML, a simple TCP socket server can return XML data. The next section illustrates this idea with the implementation of a REST-based Web service.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap