GithubHelp home page GithubHelp logo

Comments (4)

reidmorrison avatar reidmorrison commented on May 22, 2024

Good point, does not look like the API takes into consideration the use case where we just want call socket.read and just read everything until the socket is closed.
Can you check if using a regular socket works in the above scenario when calling socket.read?
If so, I will update the gem to support a read with no parameters.

from net_tcp_client.

njh avatar njh commented on May 22, 2024

Yes, you can call IO#read without any parameters:

If length is omitted or is nil, it reads until EOF and the encoding conversion is applied, if applicable. A string is returned even if EOF is encountered before any data is read.

Here is an example minimal Whois client:

TCPSocket.open("whois.nic.me", 43) do |sock|
  sock.write("DOMAIN njh.me\r\n")
  response = sock.read
  puts "Received: #{response}"
end

It would also be really useful to be able to read line by line (IO#gets / IO#each_line / IO#readlines).

Here is an example minimal HTTP client:

TCPSocket.open("www.bbc.co.uk", 80) do |sock|
  sock.write("GET / HTTP/1.0\nHost: www.bbc.co.uk\n\r\n")
  sock.each_line do |line|
    line.chomp!
    break if line.empty?
    puts "Header: #{line}"
  end
  puts "Body: #{sock.read}"
end

It reads headers line by line, until a blank line and then reads the body until EOF.

from net_tcp_client.

reidmorrison avatar reidmorrison commented on May 22, 2024

Want to submit a pull request adding both capabilities?

from net_tcp_client.

njh avatar njh commented on May 22, 2024

I have taken a quick look and it doesn't look straightforward. While the length parameter for IO#read is optional, the length parameter for IO#read_nonblock is not. I am not sure I understand what you are doing in your code well enough to work out how to add support for this.

I was having trouble with IPv6 being down for some public whois server - so I was looking at Net::TCPClient for use in the whois ruby gem. Hower the author is not keen on adding an external dependency. The problem is probably solved just by iterating over the addresses using Resolv.each_address.

from net_tcp_client.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.