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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:55:11+00:00 2026-05-30T04:55:11+00:00

Blocking berkeley sockets normally used by Ruby has one downside: if connection with remote

  • 0

Blocking berkeley sockets normally used by Ruby has one downside: if connection with remote computer is not gracefully terminated, the recv() call will wait for hours. Normally, this can be fixed by specifying receive timeout for socket. I have tried following code with Ruby 1.9.3-p0 on both Windows 7 and Ubuntu 11.10. Unfortunately, it don’t work: recv() hangs forever :(. What i’m doing wrong?

# server.rb
require 'socket'
Socket.tcp_server_loop( 1234 ) { puts( "connected" ) }

# client.rb
require 'socket'
sock = Socket.new( :INET, :STREAM )
time = if RUBY_PLATFORM =~ /mingw/ then 1000 else [ 1, 0 ].pack( 'L*' ) end
sock.setsockopt( Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, time )
sock.connect( Socket.sockaddr_in( 1234, "127.0.0.1" ) )
sock.recv( 100 )
  • 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-30T04:55:12+00:00Added an answer on May 30, 2026 at 4:55 am

    I’m not sure what you’re trying to do, but the server never writes anything. Also, because of the way you are using tcp_server_loop, the socket never gets closed but goes out of scope, which means that it will only be closed on next garbage collection. Because you are listening for data that never comes on a socket that was never closed on the server end, that must have something to do with it. Try this for the server:

    Socket.tcp_server_loop(1234) do |sock, clientinfo|
      puts "Connection!"
      begin
        sock.puts "Welcome to Wonderland"
      ensure
        sock.close
      end
    end
    

    As for the time, on Linux, I can only guess that it is a matter of padding. I think you are imagining the perfect sequential struct which doesn’t exist. You should only really use binary structs that have been returned from C functions. And if you know the option works in C/C++, you could write a simple extension that just defines one Ruby function that sets creates a socket, sets that option, and returns it. That is probably harder than it sounds, but is a reliable option.

    EDIT:

    You could use Ruby’s timeout library and create a timeout around the recv call. It would look like this now:

    require 'socket'
    require 'timeout'
    sock = Socket.new( :INET, :STREAM )
    time = if RUBY_PLATFORM =~ /mingw/ then 1000 else [ 1, 0 ].pack( 'L*' ) end
    sock.setsockopt( Socket::SOL_SOCKET, Socket::SO_RCVTIMEO, time )
    sock.connect( Socket.sockaddr_in( 1234, "127.0.0.1" ) )
    (Timeout.timeout(100) {sock.recv( 100 )}) rescue puts("Recv timed out")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know non-blocking receive is not used as much in message passing, but still
Should sockets be set to non-blocking when used with select.select in Python? What difference
Whilst asynchronous IO (non-blocking descriptors with select/poll/epoll/kqueue etc) is not the most documented thing
What's the simplest way of blocking a thread until a file has been unlocked
How do you set the timeout for blocking operations on a Ruby socket?
What is a good way of creating non-blocking methods in Scala? One way I
Twisted has a non-blocking event loop. I understand what a blocking event loop does
I am using Synapse with blocking sockets and try to simply send text to
I have a blocking priority queue which stores Objects of type Message, message has
A non blocking socket is the one where we call fcntl() method and associate

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.