-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading arbitrary number of bytes? (Whois client) #18
Comments
Good point, does not look like the API takes into consideration the use case where we just want call |
Yes, you can call IO#read without any parameters:
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. |
Want to submit a pull request adding both capabilities? |
I have taken a quick look and it doesn't look straightforward. While the length parameter for I was having trouble with IPv6 being down for some public whois server - so I was looking at |
Hi,
I am looking into how to implement a Whois client using Net::TCPClient.
However I am a bit stuck, because I can't work out how to read an arbitrary number of bytes.
The Whois protocol involves:
I guess it is quite similar to very basic HTTP.
If I try reading a large number of bytes, like this:
Then I get an error:
Because the server closed the connection before the client received the full 4096 bytes. But if I set the read size, to a low number (for example 100), then I only get back those 100 bytes.
I have considered having a loop reading a small number of bytes at a time, but I can't see how to avoid the
Net::TCPClient::ConnectionFailure
error, given that I don't know how many bytes the server is going to send.The reason for using
Net::TCPClient
at all and not just a plainTCPSocket
, is because I want to try each of the IP addresses in DNS in turn and not just fail if the first chosen IP address is down.Any help would be very appreciated.
The text was updated successfully, but these errors were encountered: