Chinaunix

标题: Ruby 构建支持多客户端的 TCP 网络服务器 [打印本页]

作者: shift_1    时间: 2011-03-25 14:32
标题: Ruby 构建支持多客户端的 TCP 网络服务器
  1. require 'socket'

  2. server = TCPServer.new(1234)

  3. loop do
  4.   Thread.start(server.accept) do |connection|
  5.     while line = connection.gets
  6.       break if line =~ /quit/
  7.       puts line
  8.       connection.puts "Received!"
  9.     end

  10.     connection.puts "Closing the connection. Bye!"
  11.     connection.close
  12.   end
  13. end
复制代码

作者: 2gua    时间: 2011-03-25 15:52
系统管理,还是比较倾向于Perl。
作者: Sevk    时间: 2012-04-09 11:09
提示: 作者被禁止或删除 内容自动屏蔽
作者: lax    时间: 2012-04-12 21:27
while loop里用select,性能和可靠性问题都很大。倾向于用eventmachine来写。




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2