Passion is like genius; a miracle.

Blog on Software, Statistics, and Quant

Ruby Programming Language Enables Concise Network Programming


Ruby Programming Language Enables Concise Network Programming

1. 웹 서버

require 'socket'
server ='', 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>"

2. URL을 파일처럼

require 'open-uri' # allows the use of a file like API for URLs
open("") { |file|
  lines =
  puts lines

3. WEBrick Webserver

require 'webrick'
include WEBrick

s = => 9090,
                   :DocumentRoot => Dir::pwd + "/htdocs")
trap("INT"){ s.shutdown }

4. XML-RPC 클라이언트

require 'xmlrpc/client'
server ="", "/RPC2", 9090)
puts"upper_case", "The fat dog chased the cat on Elm Street.")
puts"lower_case", "The fat dog chased the cat on Elm Street.")

5. 4번의 클라이언트가 접속 가능한 XML-RPC 서버

require 'webrick'
require 'xmlrpc/server.rb'

# create a servlet to handle XML-RPC requests:
servlet =
servlet.add_handler("upper_case") { |a_string| a_string.upcase }
servlet.add_handler("lower_case") { |a_string| a_string.downcase }

# create a WEBrick instance to host this servlet: => 9090)
trap("INT"){ server.shutdown }
server.mount("/RPC2", servlet)

6. XML Webservices 클라이언트

require 'soap/rpc/driver'
stub ="", "")
stub.add_method('upper_case', 'a_string')
stub.add_method('lower_case', 'a_string')
stub.add_method('times_string', 'a_string', 'a_number')

puts stub.lower_case("Jack and Jill went up the hill.")
puts stub.upper_case("Jack and Jill went up the hill.")
puts stub.times_string("Jack and Jill went up the hill.", 2)

7. 6번의 클라이언트가 접속할 수 있는 XML Webservices 서버

require ‘soap/rpc/standaloneserver’

# define a class that has three methods to call remotely:

class DemoClass
def upper_case(a_string)
def lower_case(a_string)
def times_string(a_string, a_number)
a_string * a_number

# create a SOAP enabled server for the methods in DemoClass:

class MyServer < SOAP::RPC::StandaloneServer def on_init demo = add_method(demo, "upper_case", "a_string") add_method(demo, "lower_case", "a_string") add_method(demo, "times_string", "a_string", "a_number") end end server ="Demo", "", "", 9090) trap('INT') { server.shutdown } server.start [/code]


Leave a Reply

Your email address will not be published. Required fields are marked *