Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8877661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:31:09+00:00 2026-06-14T19:31:09+00:00

I have a 2 part question on reading from sockets and how is it

  • 0

I have a 2 part question on reading from sockets and how is it managed on Ruby servers like Unicorn or Mongrel

  1. I’ve learnt that to read from a socket is different from reading a file and that there are no distinct EOF message sent and the data is an endless stream. So how do you know when to stop reading? My TCPServer for example in this case when I hit my server by accessing http://localhost:9799 from a browser, it hangs after there is no more data to read and it won’t throw the EOFError either.

require 'socket'

READ_CHUNK = 1024
socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)
addr = Socket.pack_sockaddr_in(9799, '127.0.0.1')
socket.bind(addr)
socket.listen(Socket::SOMAXCONN)
socket.setsockopt(:SOCKET, :REUSEADDR, true)

puts "Server is listening on port = 9799"
loop do 
    connection, addr_info = socket.accept
    data_buffer = ""

    loop do
        begin
            connection.read_nonblock(READ_CHUNK, data_buffer)
            puts "Buffer = #{data_buffer}"
        rescue Errno::EAGAIN => e
            IO.select([connection])         
            retry
        rescue EOFError
            break
        end
    end
    connection.write("HTTP/1.1 200 \r\n")
    connection.write("Content-Type: text/html\r\n")
    connection.write("Status 200 \r\n")
    connection.write("Connection: close \r\n")
    connection.write("Hello World \r\n")
    connection.close
end

I’d like to know whats the best practice/standard approach used by Ruby servers. I see the Unicorn uses read_nonblock from kgio library and mongrel uses readpartial (I’m not sure about these but going through the code this is what I feel is the approach adopted.) Even with checks for \r\n how does the server know the input is complete.
Could explain how this should be done (and I think gets is not the approach – its with read, readpartial, read_nonblock).

2). I would really appreciate a few lines on how this is achieved in servers like unicorn or passenger

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:31:10+00:00Added an answer on June 14, 2026 at 7:31 pm

    It’s done in unicorn here
    https://github.com/defunkt/unicorn/blob/master/lib/unicorn/http_request.rb#L69-L71

    There is add_parse method(read the comments above methods)
    https://github.com/defunkt/unicorn/blob/master/ext/unicorn_http/unicorn_http.rl#L760-L778

    Also take a look at some explanations here http://www.ruby-forum.com/topic/2267632#1014288

    Here is your working code using http_parser.rb https://gist.github.com/4136962

    gem install http_parser.rb

    require 'socket'
    require "http/parser"
    
    
    READ_CHUNK = 1024 * 4
    socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)
    addr = Socket.pack_sockaddr_in(9799, '127.0.0.1')
    socket.bind(addr)
    socket.listen(Socket::SOMAXCONN)
    socket.setsockopt(:SOCKET, :REUSEADDR, true)
    
    puts "Server is listening on port = 9799"
    loop do
        connection, addr_info = socket.accept
    
        parser = Http::Parser.new
        begin
          data = connection.readpartial(READ_CHUNK)
          puts "Buffer = #{data}"
          parser << data
        end until parser.headers
    
        connection.write("HTTP/1.1 200 \r\n")
        connection.write("Content-Type: text/html\r\n")
        connection.write("Status 200 \r\n")
        connection.write("Connection: close \r\n")
        connection.write("\r\n\r\n")
        connection.write("Hello World \r\n")
        connection.close
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have part of my script that goes like this: while read line do
This is probably a multi-part question. Background: we have a native (c++) library that
I have seen this question that talks about getting the last part of a
I have a question about reading bytes from server socket. I'm using the default
I have a three part question based on a dataframe (df is example rows)
Multiple part question on running gvim on windows xp. I have a _vimrc file
This is two part question: I have two stored procedures: sp1 & sp2. If
i have this simple question please. I have this part of code which sets
I have a question about extracting a part of a string. For example I
I have a question concerning active record association, referring to this part of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.