mirror of
				https://github.com/balkian/SWCM.git
				synced 2025-11-04 09:38:20 +00:00 
			
		
		
		
	Added concurrent server in ruby 0.1
This commit is contained in:
		
							
								
								
									
										45
									
								
								ServidorConcurrente/servidor.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								ServidorConcurrente/servidor.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
require 'socket'
 | 
			
		||||
 | 
			
		||||
numCon = 0
 | 
			
		||||
maxCon = 10
 | 
			
		||||
 | 
			
		||||
def respond(socket, code, body)   # Genera prespuestas
 | 
			
		||||
  socket.puts "HTTP/1.0 #{ code }"
 | 
			
		||||
  socket.puts "Content-type: text/html"
 | 
			
		||||
  socket.puts "Content-length: #{ body.length }"
 | 
			
		||||
  socket.puts
 | 
			
		||||
  socket.puts body
 | 
			
		||||
  socket.close
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
server = TCPServer.open(8080)
 | 
			
		||||
puts "web server created at port 8080"
 | 
			
		||||
loop do
 | 
			
		||||
  if(numCon < maxCon)
 | 
			
		||||
	sock = server.accept				# Acepta conexi<78>n del cliente
 | 
			
		||||
	t = Thread.new(sock) do |socket|
 | 
			
		||||
		numCon=numCon+1
 | 
			
		||||
		request = socket.gets            # Lee la primera l<>nea
 | 
			
		||||
		o = socket.gets until ( o != "") # cabecera acaba con l<><6C>nea vac<61>a
 | 
			
		||||
 | 
			
		||||
		unless request =~ /^GET/          # Si el comando no esta soportado
 | 
			
		||||
			respond socket, "400 Bad Request", "<html><body><h1>Comando desconocido</h1></body></html>" 
 | 
			
		||||
			next                            # Acaba el ciclo
 | 
			
		||||
		end
 | 
			
		||||
		request =~ /^GET \/(.*) .*/       # GET /(fichero) HTTP/1.x
 | 
			
		||||
		path = $1
 | 
			
		||||
		if path == "" || path =~ /\/$/ then path += "index.html" end  # Inserta index.html
 | 
			
		||||
		if File.exists?(path)
 | 
			
		||||
			file = open(path)               # Lee fichero y prepara respuesta
 | 
			
		||||
			puts "OK #{ path }"
 | 
			
		||||
			respond socket, "200 OK", file.read
 | 
			
		||||
		else
 | 
			
		||||
			puts "NotFound #{ path }"       # Si el fichero no existe
 | 
			
		||||
			respond socket, "404 Not Found", "<html><body><h1>Recurso no encontrado</h1></body></html>" 
 | 
			
		||||
		end
 | 
			
		||||
		numCon=numCon-1;
 | 
			
		||||
	  end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user