TCPServer

TCPServer

refe

 ==== TCPServer ====
 TCP/IPストリーム型接続のサーバ側のソケットのクラス。このクラ
 スによって簡単にソケットを利用したサーバプログラミングがで
 きます。例えばechoサーバは以下のようになります。
 
   require "socket"
 
   gs = TCPServer.open(0)
   socks = [gs]
   addr = gs.addr
   addr.shift
   printf("server is on %s\n", addr.join(":"))
 
   while true
     nsock = select(socks)
     next if nsock == nil
     for s in nsock[0]
       if s == gs
         socks.push(s.accept)
         print(s, " is accepted\n")
       else
         if s.eof?
           print(s, " is gone\n")
           s.close
           socks.delete(s)
         else
           str = s.gets
           s.write(str)
         end
       end
     end
   end
 
 Threadを使えばもっと短くなります。
 
   require "socket"
 
   gs = TCPServer.open(0)
   addr = gs.addr
   addr.shift
   printf("server is on %s\n", addr.join(":"))
 
   while true
     Thread.start(gs.accept) do |s|       # save to dynamic variable
       print(s, " is accepted\n")
       while s.gets
         s.write($_)
       end
       print(s, " is gone\n")
       s.close
     end
   end
 ---- Singleton methods ----
 new open
 ---- Instance methods ----
 accept listen sysaccept
 ---- Singleton methods (inherited) ----
 APPEND BINARY CREAT EXCL FNM_CASEFOLD FNM_DOTMATCH
 FNM_NOESCAPE FNM_PATHNAME FNM_PERIOD LOCK_EX LOCK_NB
 LOCK_SH LOCK_UN NOCTTY NONBLOCK RDONLY RDWR SEEK_CUR
 SEEK_END SEEK_SET SYNC TRUNC WRONLY do_not_reverse_lookup
 do_not_reverse_lookup= for_fd foreach getaddress pipe popen
 read readlines select sysopen
 ---- Instance methods (inherited) ----
 << == === =~ __id__ __send__ _dump _load addr all? any?
 binmode class clone close close_read close_write closed?
 collect detect display dup each each_byte each_line
 each_with_index entries eof eof? eql? equal? extend fcntl
 fileno find find_all flush freeze frozen? fsync getc
 getpeername gets getsockname getsockopt grep hash id
 include? initialize initialize_copy inject inspect
 instance_eval instance_of? instance_variable_get
 instance_variable_set instance_variables ioctl is_a? isatty
 kind_of? lineno lineno= map marshal_dump marshal_load max
 member? method method_missing methods min nil? object_id
 partition peeraddr pid pos pos= pretty_print
 pretty_print_cycle pretty_print_instance_variables print
 printf private_methods protected_methods public_methods
 putc puts read readchar readline readlines recv recvfrom
 reject remove_instance_variable reopen respond_to? rewind
 seek select send setsockopt shutdown singleton_method_added
 singleton_method_removed singleton_method_undefined
 singleton_methods sort sort_by stat sync sync= sysread
 sysseek syswrite taint tainted? tell to_a to_ary to_hash
 to_i to_int to_io to_s to_str tty? type ungetc untaint
 write zip

* はてなダイアリーキーワード:TCPServer

  • 2006/05/15 12:37:57 list
  • 2006/05/15 12:42:14 new
  • 2006/05/15 12:44:04 open
  • 2006/05/15 11:48:27 Socket
  • 2006/05/15 11:51:24 TCPServer
  • 2006/05/15 11:59:52 ^