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 5959863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:44:40+00:00 2026-05-22T18:44:40+00:00

I have some code that I’m using to get data from a network socket.

  • 0

I have some code that I’m using to get data from a network socket. It works fine, but I flailed my way into it through trial and error. I humbly admit that I don’t fully understand how it works, but I would really like to. (This was cargo culted form working code I found)

The part I don’t understand starts with “ready = IO.select …” I’m unclear on:

  1. What IO.select is doing (I tried looking it up but got even more confused with Kernel and what-not)
  2. what the array argument to IO.select is for
  3. what ready[0] is doing
  4. the general idea of reading 1024 bytes? at a time

Here’s the code:

@mysocket = TCPSocket.new('192.168.1.1', 9761)

th = Thread.new do
    while true
        ready = IO.select([@mysocket])
        readable = ready[0]

        readable.each do |socket|
            if socket == @mysocket
                buf = @mysocket.recv_nonblock(1024)
                if buf.length == 0
                    puts "The server connection is dead. Exiting."
                    exit
                else
                    puts "Received a message"
                end
            end
        end

    end
end

Thanks in advance for helping me “learn to fish”. I hate having bits of my code that I don’t fully understand – it’s just working by coincidence.

  • 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-05-22T18:44:41+00:00Added an answer on May 22, 2026 at 6:44 pm

    1) IO.select takes a set of sockets and waits until it’s possible to read or write with them (or if error happens). It returns sockets event happened with.

    2) array contains sockets that are checked for events. In your case you specify only sockets for reading.

    3) IO.select returns an array of arrays of sockets. Element 0 contains sockets you can read from, element 1 – sockets you can write to and element 2 – sockets with errors.

    After getting list of sockets you can read the data.

    4) yes, recv_nonblock argument is size in byte. Note that size of data actually being read may be less than 1024, in this case you may need to repeat select (if actual data matters for you).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that looks like this that works just fine: var info
I have some code that is looking up group memberships from local groups on
I have some code that I am using to count characters in an input/textarea
I have some code that I want to execute as follows. But I keep
We have some code that removes dangerous attributes and tags from HTML. I noticed
I have some code that looks like the following coming back from an XHR
I have some code that parses an xml file using XDocument . It retrieves
I have some code that I have created for an OnChange event which works
I have some code that is structured as follows from my.modules import MyClass Class
I have some code that gives a user id to a utility that then

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.