Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I properly handle persistent TCP socket connections (to simulate an HTTP server)?
    text
    copied!<p>So, I'm trying to simulate some basic HTTP persistent connections using sockets and Ruby - for a college class.</p> <p>The point is to build a server - able to handle multiple clients - that receives a file path and gives back the file content - just like an HTTP GET.</p> <p>The current server implementation loops listening for clients, fires a new thread when there's an incoming connection and reads the file paths from this socket. It's very dumb, but it works fine when working with non-presistent connections - one request per connection.</p> <p><strong>But they should be persistent.</strong></p> <p>Which means the client shouldn't worry about closing the connection. In the non-persistent version the servers echoes the response and close the connection - <em>goodbye client, farewell</em>. But being persistent means the server thread should loop and wait for more incoming requests until... well until there's no more requests. How does the server knows that? It doesn't! Some sort of timeout is needed. I tried to do that with Ruby's Timeout, but it didn't work.</p> <p>Googling for some solutions - besides being thoroughly advised to avoid using Timeout module - I've seen a lot of posts about the IO.select method, that should handle this socket waiting issue way better than using threads and stuff (which really sounds cool, considering how Ruby threads (don't) work). I'm trying to understand here how IO.select works, but still wasn't able to make it work in the current scenario.</p> <p>So I aske basically two things:</p> <ul> <li><p>how can I efficiently work this timeout issue on the server-side, either using some thread based solution, low-level socket options or some IO.select magic?</p></li> <li><p>how can the client side know that the server has closed its side of the connection?</p></li> </ul> <p>Here's the current code for the server:</p> <pre><code>require 'date' module Sockettp class Server def initialize(dir, port = Sockettp::DEFAULT_PORT) @dir = dir @port = port end def start puts "Starting Sockettp server..." puts "Serving #{@dir.yellow} on port #{@port.to_s.green}" Socket.tcp_server_loop(@port) do |socket, client_addrinfo| handle socket, client_addrinfo end end private def handle(socket, addrinfo) Thread.new(socket) do |client| log "New client connected" begin loop do if client.eof? puts "#{'-' * 100} end connection" break end input = client.gets.chomp body = content_for(input) response = {} if body response.merge!({ status: 200, body: body }) else response.merge!({ status: 404, body: Sockettp::STATUSES[404] }) end log "#{addrinfo.ip_address} #{input} -- #{response[:status]} #{Sockettp::STATUSES[response[:status]]}".send(response[:status] == 200 ? :green : :red) client.puts(response.to_json) end ensure socket.close end end end def content_for(path) path = File.join(@dir, path) return File.read(path) if File.file?(path) return Dir["#{path}/*"] if File.directory?(path) end def log(msg) puts "#{Thread.current} -- #{DateTime.now.to_s} -- #{msg}" end end end </code></pre> <p><strong>Update</strong></p> <p>I was able to simulate the timeout behaviour using the IO.select method, but the implementation doesn't feel good when combining with a couple of threads for accepting new connections and another couple for handling requests. The concurrency makes the situation mad and unstable, and I'm probably not sticking with it unless I can figure out a better way of using this solution.</p> <p><strong>Update 2</strong></p> <p>Seems like Timeout is still the best way to handle this. I'm sticking with it till find a better option. I still don't know how to deal with zombie client connections.</p> <p><strong>Solution</strong></p> <p>I endend up using IO.select (got inspired when looking at the webrick code). You cha check the final version <a href="http://github.com/fuadsaud/http/" rel="nofollow">here</a> (lib/http/server/client_handler.rb)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload