sample/svr.rb


DEFINITIONS

This source file includes following functions.


   1  # socket example - server side
   2  # usage: ruby svr.rb
   3  
   4  require "socket"
   5  
   6  gs = TCPserver.open(0)
   7  addr = gs.addr
   8  addr.shift
   9  printf("server is on %s\n", addr.join(":"))
  10  socks = [gs]
  11  
  12  while TRUE
  13    nsock = select(socks);
  14    next if nsock == nil
  15    for s in nsock[0]
  16      if s == gs
  17        ns = s.accept
  18        socks.push(ns)
  19        print(s, " is accepted\n")
  20      else
  21        if s.eof?
  22          print(s, " is gone\n")
  23          s.close
  24          socks.delete(s)
  25        else
  26          if str = s.gets;
  27            s.write(str)
  28          end
  29        end
  30      end
  31    end
  32  end