To create a simple Web server, you can accept connections on the IP address 0.0.0.0 and the port number 80 : TCP Server « Network « Ruby






To create a simple Web server, you can accept connections on the IP address 0.0.0.0 and the port number 80


require 'socket'
server = TCPServer.new("0.0.0.0", 80)
loop do
  socket = server.accept
while socket.gets.chop.length > 0
end
socket.puts "HTTP/1.1 200 OK"
socket.puts "Content-type: text/html"
socket.puts ""
socket.puts "<html>"
socket.puts "<body>"
socket.puts "<center>"
socket.puts "<h1>#{Time.now}</h1>"
socket.puts "</center>"
socket.puts "</body>"
socket.puts "</html>"
socket.close
end

 








Related examples in the same category

1.Building a Simple TCP Server
2.Multi-Client TCP Servers
3.Creating a Web Server with thread support