1998-01-16 15:13:05 +03:00
|
|
|
# socket example - server side using thread
|
|
|
|
# usage: ruby tsvr.rb
|
|
|
|
|
|
|
|
require "socket"
|
|
|
|
|
2009-08-03 13:26:26 +04:00
|
|
|
gs = TCPServer.open(0)
|
1998-01-16 15:13:05 +03:00
|
|
|
addr = gs.addr
|
|
|
|
addr.shift
|
2000-11-16 10:24:21 +03:00
|
|
|
printf("server is on %s\n", addr.join(":"))
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-12-19 23:56:37 +03:00
|
|
|
loop do
|
2000-06-22 12:29:58 +04:00
|
|
|
Thread.start(gs.accept) do |s|
|
|
|
|
print(s, " is accepted\n")
|
2002-12-19 23:56:37 +03:00
|
|
|
while line = s.gets
|
|
|
|
s.write(line)
|
1998-01-16 15:13:05 +03:00
|
|
|
end
|
|
|
|
print(s, " is gone\n")
|
|
|
|
s.close
|
|
|
|
end
|
|
|
|
end
|