2001-05-26 07:38:39 +04:00
|
|
|
# simple httpd
|
|
|
|
|
|
|
|
# The code demonstrates how a multi-protocol daemon should be written.
|
|
|
|
|
|
|
|
require "socket"
|
|
|
|
require "thread"
|
|
|
|
|
|
|
|
port = 8888
|
|
|
|
res = Socket.getaddrinfo(nil, port, nil, Socket::SOCK_STREAM, nil, Socket::AI_PASSIVE)
|
|
|
|
sockpool = []
|
|
|
|
names = []
|
|
|
|
threads = []
|
|
|
|
|
|
|
|
res.each do |i|
|
2009-08-03 13:26:26 +04:00
|
|
|
s = TCPServer.new(i[3], i[1])
|
2001-05-26 07:38:39 +04:00
|
|
|
n = Socket.getnameinfo(s.getsockname, Socket::NI_NUMERICHOST|Socket::NI_NUMERICSERV).join(" port ")
|
|
|
|
sockpool.push s
|
|
|
|
names.push n
|
|
|
|
end
|
|
|
|
|
|
|
|
(0 .. sockpool.size - 1).each do |i|
|
|
|
|
mysock = sockpool[i]
|
|
|
|
myname = names[i]
|
|
|
|
STDERR.print "socket #{mysock} started, address #{myname}\n"
|
|
|
|
threads[i] = Thread.start do # Thread.start cannot be used here!
|
|
|
|
ls = mysock # copy to dynamic variable
|
|
|
|
t = Thread.current
|
|
|
|
STDERR.print "socket #{myname} listener started, pid #{$$} thread #{t}\n"
|
2007-04-16 17:53:09 +04:00
|
|
|
while true
|
2001-05-26 07:38:39 +04:00
|
|
|
as = ls.accept
|
|
|
|
Thread.start do
|
2013-07-20 17:41:02 +04:00
|
|
|
STDERR.print "socket #{myname} accepted, thread ", Thread.current, "\n"
|
|
|
|
s = as # copy to dynamic variable
|
|
|
|
str = ''
|
|
|
|
while line = s.gets
|
|
|
|
break if line == "\r\n" or line == "\n"
|
|
|
|
str << line
|
|
|
|
end
|
|
|
|
STDERR.print "socket #{myname} got string\n"
|
|
|
|
s.write("HTTP/1.0 200 OK\n")
|
|
|
|
s.write("Content-type: text/plain\n\n")
|
|
|
|
s.write("this is test: my name is #{myname}, you sent:\n")
|
|
|
|
s.write("---start\n")
|
|
|
|
s.write(str)
|
|
|
|
s.write("---end\n")
|
|
|
|
s.close
|
|
|
|
STDERR.print "socket #{myname} processed, thread ", Thread.current, " terminating\n"
|
2001-05-26 07:38:39 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for t in threads
|
|
|
|
t.join
|
|
|
|
end
|