1# socket example - server side 2# usage: ruby svr.rb 3 4# this server might be blocked by an ill-behaved client. 5# see tsvr.rb which is safe from client blocking. 6 7require "socket" 8 9gs = TCPServer.open(0) 10addr = gs.addr 11addr.shift 12printf("server is on %s\n", addr.join(":")) 13socks = [gs] 14 15loop do 16 nsock = select(socks); 17 next if nsock == nil 18 for s in nsock[0] 19 if s == gs 20 ns = s.accept 21 socks.push(ns) 22 print(s, " is accepted\n") 23 else 24 if s.eof? 25 print(s, " is gone\n") 26 s.close 27 socks.delete(s) 28 # single thread gets may block whole service 29 elsif str = s.gets 30 s.write(str) 31 end 32 end 33 end 34end 35