socket error handling... Net http

  • Thread starter Bigmac Turdsplash
  • Start date
B

Bigmac Turdsplash

loop {
res =
Net::HTTP.get_response(URI.parse('http://bigmac.t35.com/text.txt'))
print res.body
sleep 3
}


this loop brakes when i get the (SocketError)... i get errors because of
bad connection to my wireless network... i dont know how to handle this
error and keep the script from crashing...


:getaddrinfo: no address associated with hostname. (SocketError)
:in `open'
:in `connect
:in `timeout'
:in `timeout'
:in `connect
:in `do_star
:in `start'
 
R

Robert Klemme

loop {
res =
Net::HTTP.get_response(URI.parse('http://bigmac.t35.com/text.txt'))
print res.body
sleep 3
}


this loop brakes when i get the (SocketError)... i get errors because of
bad connection to my wireless network... i dont know how to handle this
error and keep the script from crashing...

I suggest you read about Ruby's exception handling mechanisms. For a
start please look here:

http://ruby-doc.org/docs/ProgrammingRuby/

Kind regards

robert
 
B

Brian Candler

7stud said:
require 'socket'

And how exactly will that help? Especially since net/http.rb already
does

require 'net/protocol'

and net/protocol.rb does

require 'socket'

??

*Read* the error message. It says:

"getaddrinfo: no address associated with hostname"

That is: there was an error in resolving a name to an IP address. Not
that a Ruby constant hadn't been defined.
 
D

Douglas Seifert

[Note: parts of this message were removed to make it a legal post.]
loop {
res =
Net::HTTP.get_response(URI.parse('http://bigmac.t35.com/text.txt'<http://bigmac.t35.com/text.txt'>
))
print res.body
sleep 3
}


this loop brakes when i get the (SocketError)... i get errors because of
bad connection to my wireless network... i dont know how to handle this
error and keep the script from crashing...

Try this:

loop do
begin
res = Net::HTTP.get_response(URI.parse("http://bigmac.t35.com/text.txt
"))
print res.body
rescue SocketError => se
puts "Got socket error: #{se}"
end
sleep 3
end
 
M

Marc Heiler

i dont know how to handle this error and keep the script from crashing.

As already mentioned here (and code provided above), one strategy is to
use
begin/rescue:

begin
some_code_here_which_can_sometimes_fail
rescue # or rescue specific errors like LoadError... or in more
# general rescue Exception => e; pp e
# ^^^ is what I often do when i just quickly want to get info. i
love pp
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top