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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:49:11+00:00 2026-06-05T16:49:11+00:00

I am following the tutorial: http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm and using it to set up the Simple

  • 0

I am following the tutorial: http://www.tutorialspoint.com/ruby/ruby_socket_programming.htm and using it to set up the “Simple Client”.

The code is as follows:

    require 'socket'      # Sockets are in standard library

    hostname = 'localhost'
    port = 2000

    s = TCPSocket.open(host, port)

    while line = s.gets   # Read lines from the socket
      puts line.chop      # And print with platform line terminator
    end
    s.close               # Close the socket when done

When I execute a single s.gets statement, I get the first response line from the server. When I do s.gets again, I get the second response line, etc. However, the moment I get to the end of the response and do s.gets then the program goes into a frozen state. (This is if I test the above program from the Rails Console). The same happens when I run the code – the freeze occurs when the while statement does the final s.gets. Any ideas on how to solve this?

For information: The server is a C# server that sends response messages using the following C# code (each line of the response uses the same code and the final line in the response is not treated any differently from the other lines):

    socket.Send(Encoding.ASCII.GetBytes(msg + "\r\n"));

So I am thinking the client freezes because it probably doesn’t interpret the servers “end of response” properly. Any ideas?

  • 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-05T16:49:12+00:00Added an answer on June 5, 2026 at 4:49 pm

    It seems like there is a problem with reading the nil value from the socket. Still not sure why this happens, but using a selector as suggested in Recovering from a broken TCP socket in Ruby when in gets() solves the problem very nicely (would have upvoted his answer, but don’t have enough rep).

    Solution I followed was to add the selector in a method as follows:

        def next_line_readable?(socket)
          readfds, writefds, exceptfds = select([socket], nil, nil, 0.1)
          p :r => readfds, :w => writefds, :e => exceptfds
          readfds #Will be nil if next line cannot be read
        end
    

    and then in main program to use it like:

        socket = TCPSocket.open(host, port)
        while next_line_readable?(socket)
          puts socket.gets.chop
        end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the Developer edition of Salesforce, following this tutorial: http://www.salesforce.com/us/developer/docs/fundamentals/index.htm I've got to
I am following the following tutorial ( http://www.highoncoding.com/Articles/642_Creating_a_Stock_Widget_in_ASP_NET_MVC_Application.aspx ) on using ajax to render
I am following this tutorial: http://www.silverlighthack.com/post/2010/03/21/Using-the-Silverlight-Bing-Maps-control-on-the-Windows-Phone-7.aspx Task 5 indicates that if I add System.Windows.Browser
i have made a simple php contact form following this tutorial: http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme The big
Hi I am following this tutorial: http://www.mastertheboss.com/hibernate/182-hibernate-tutorial.html I have done all the code so
I have created iphone bluetooth chat application using following tutorial:http://www.devx.com/wireless/Article/43502/1954. It works well.But when
I am following the tutorial here: http://www.aspfree.com/c/a/XML/Applying-XSLT-to-XML-Using-ASP.NET/2/ The tutorial shows how one converts xml
I'm using Ubuntu 10, python 2.6.5 I'm following this tutorial: http://www.djangobook.com/en/2.0/chapter02 I followed all
I'm working with the following tutorial: http://www.ibm.com/developerworks/opensource/library/os-ws-rubyrails/index.html Everything after step 7 there results in
Following this tutorial (http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application), I learned how to save data and do concurrency checks

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.