advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Average Rating: 5/5 | Rate this item | 3 users have rated this item.
 Print Print
 
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.

Page 1 of 4
advertisement
  Next Page: Implement a REST-Based Web Service
Page 1: IntroductionPage 3: Implement an XML-RPC-Based Web Service
Page 2: Implement a REST-Based Web ServicePage 4: Implement a SOAP-Based Web Service
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES