summaryrefslogtreecommitdiff
path: root/trunk/sample/svr.rb
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-25 15:02:05 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-08-25 15:02:05 +0000
commit0dc342de848a642ecce8db697b8fecd83a63e117 (patch)
tree2b7ed4724aff1f86073e4740134bda9c4aac1a39 /trunk/sample/svr.rb
parentef70cf7138ab8034b5b806f466e4b484b24f0f88 (diff)
added tag v1_9_0_4
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v1_9_0_4@18845 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'trunk/sample/svr.rb')
-rw-r--r--trunk/sample/svr.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/trunk/sample/svr.rb b/trunk/sample/svr.rb
new file mode 100644
index 0000000000..11cfc5fbfe
--- /dev/null
+++ b/trunk/sample/svr.rb
@@ -0,0 +1,34 @@
+# socket example - server side
+# usage: ruby svr.rb
+
+# this server might be blocked by an ill-behaved client.
+# see tsvr.rb which is safe from client blocking.
+
+require "socket"
+
+gs = TCPserver.open(0)
+addr = gs.addr
+addr.shift
+printf("server is on %s\n", addr.join(":"))
+socks = [gs]
+
+loop do
+ nsock = select(socks);
+ next if nsock == nil
+ for s in nsock[0]
+ if s == gs
+ ns = s.accept
+ socks.push(ns)
+ print(s, " is accepted\n")
+ else
+ if s.eof?
+ print(s, " is gone\n")
+ s.close
+ socks.delete(s)
+ # single thread gets may block whole service
+ elsif str = s.gets
+ s.write(str)
+ end
+ end
+ end
+end