summaryrefslogtreecommitdiffstats
path: root/sample
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-05-26 03:38:39 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-05-26 03:38:39 +0000
commitcfd4be1471c963aba2c81f0969a3ff3cdef1a9e7 (patch)
treea12fdf6faf528343d284ec5848a6bf779841204e /sample
parent5c557e6889a83b39d8bb24d08ed1bf4dc291aaf4 (diff)
downloadruby-cfd4be1471c963aba2c81f0969a3ff3cdef1a9e7.tar.gz
ruby-cfd4be1471c963aba2c81f0969a3ff3cdef1a9e7.tar.xz
ruby-cfd4be1471c963aba2c81f0969a3ff3cdef1a9e7.zip
Synchronize MANIFEST with reality. Add missing files.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@1463 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample')
-rw-r--r--sample/dualstack-fetch.rb48
-rw-r--r--sample/dualstack-httpd.rb55
-rw-r--r--sample/io.rb44
3 files changed, 103 insertions, 44 deletions
diff --git a/sample/dualstack-fetch.rb b/sample/dualstack-fetch.rb
new file mode 100644
index 000000000..ab8d0914f
--- /dev/null
+++ b/sample/dualstack-fetch.rb
@@ -0,0 +1,48 @@
+# simple webpage fetcher
+
+# The code demonstrates how a multi-protocol client should be written.
+# TCPsocket is using getaddrinfo() internally, so there should be no problem.
+
+require "socket"
+
+if ARGV.size != 1
+ STDERR.print "requires URL\n"
+ exit
+end
+
+url = ARGV[0]
+if url !~ /^http:\/\/([^\/]+)(\/.*)$/
+ STDERR.print "only http with full hostname is supported\n"
+ exit
+end
+
+# split URL into host, port and path
+hostport = $1
+path = $2
+if (hostport =~ /^(.*):([0-9]+)$/)
+ host = $1
+ port = $2
+else
+ host = hostport
+ port = 80
+end
+if host =~ /^\[(.*)\]$/
+ host = $1
+end
+
+#STDERR.print "url=<#{ARGV[0]}>\n"
+#STDERR.print "host=<#{host}>\n"
+#STDERR.print "port=<#{port}>\n"
+#STDERR.print "path=<#{path}>\n"
+
+STDERR.print "conntecting to #{host} port #{port}\n"
+c = TCPsocket.new(host, port)
+dest = Socket.getnameinfo(c.getpeername,
+ Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV)
+STDERR.print "conntected to #{dest[0]} port #{dest[1]}\n"
+c.print "GET #{path} HTTP/1.0\n"
+c.print "Host: #{host}\n"
+c.print "\n"
+while c.gets
+ print
+end
diff --git a/sample/dualstack-httpd.rb b/sample/dualstack-httpd.rb
new file mode 100644
index 000000000..893b29feb
--- /dev/null
+++ b/sample/dualstack-httpd.rb
@@ -0,0 +1,55 @@
+# simple httpd
+
+# The code demonstrates how a multi-protocol daemon should be written.
+
+require "socket"
+require "thread"
+
+port = 8888
+res = Socket.getaddrinfo(nil, port, nil, Socket::SOCK_STREAM, nil, Socket::AI_PASSIVE)
+sockpool = []
+names = []
+threads = []
+
+res.each do |i|
+ s = TCPserver.new(i[3], i[1])
+ n = Socket.getnameinfo(s.getsockname, Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV).join(" port ")
+ sockpool.push s
+ names.push n
+end
+
+(0 .. sockpool.size - 1).each do |i|
+ mysock = sockpool[i]
+ myname = names[i]
+ STDERR.print "socket #{mysock} started, address #{myname}\n"
+ threads[i] = Thread.start do # Thread.start cannot be used here!
+ ls = mysock # copy to dynamic variable
+ t = Thread.current
+ STDERR.print "socket #{myname} listener started, pid #{$$} thread #{t}\n"
+ while TRUE
+ as = ls.accept
+ Thread.start do
+ STDERR.print "socket #{myname} accepted, thread ", Thread.current, "\n"
+ s = as # copy to dynamic variable
+ str = ''
+ while line = s.gets
+ break if line == "\r\n" or line == "\n"
+ str << line
+ end
+ STDERR.print "socket #{myname} got string\n"
+ s.write("HTTP/1.0 200 OK\n")
+ s.write("Content-type: text/plain\n\n")
+ s.write("this is test: my name is #{myname}, you sent:\n")
+ s.write("---start\n")
+ s.write(str)
+ s.write("---end\n")
+ s.close
+ STDERR.print "socket #{myname} processed, thread ", Thread.current, " terminating\n"
+ end
+ end
+ end
+end
+
+for t in threads
+ t.join
+end
diff --git a/sample/io.rb b/sample/io.rb
deleted file mode 100644
index 0b38d2112..000000000
--- a/sample/io.rb
+++ /dev/null
@@ -1,44 +0,0 @@
-# IO test
-# usage: ruby io.rb file..
-
-home = ENV["HOME"]
-home.sub("m", "&&")
-print(home, "\n")
-print(home.reverse, "\n")
-
-if File.s("io.rb")
- print(File.s("io.rb"), ": io.rb\n")
-end
-
-$/="f\n"
-for i in "abc\n\ndef\nghi\n"
- print("tt: ", i)
-end
-
-printf("%s:(%d)%s\n", $0, ARGV.length, ARGV[0])
-passwd = open(ARGV[0], "r")
-#printf("%s", passwd.find{i|i =~ /\*/})
-
-n = 1
-for i in passwd #.grep(/^\*/)
- printf("%6d: %s", n, i)
- n = n + 1;
-end
-
-fp = open("|-", "r")
-
-if fp == nil
- for i in 1..5
- print(i, "\n")
- end
-else
- for line in fp
- print(line)
- end
-end
-
-def printUsage()
- if $USAGE
- apply($USAGE);
- end
-end