UDPSocket - script crash

M

mark shennce

hello,

if there is no peer for this udp socket which
was created with

$sock = UDPSocket.new

the script crash with the message
'connection was closed by peer'.

How to avoid this crash?

def Send str
begin
$sock.send(str, 0, "127.0.0.1", 9999) # *** crash if no peer...
rescue Exception
p "Oops" # was never called
end

$trace.insert(0.0, sprintf("=> %s", str))
end


mark
 
M

mark shennce

mark said:
hello,

if there is no peer for this udp socket which
was created with

$sock = UDPSocket.new

the script crash with the message
'connection was closed by peer'.

How to avoid this crash?

def Send str
begin
$sock.send(str, 0, "127.0.0.1", 9999) # *** crash if no peer...
rescue Exception
p "Oops" # was never called
end

$trace.insert(0.0, sprintf("=> %s", str))
end


mark


yeah,

I use ruby 1.8.1 and winnt

m
 
B

Brian Candler

if there is no peer for this udp socket which
was created with

$sock = UDPSocket.new

the script crash with the message
'connection was closed by peer'.

How to avoid this crash?

Can you give a *complete* program which causes it, rather than fragments?
The following program works fine for me (FreeBSD)

-------------------------------------------------------------------
require 'socket'
def mysend(str)
begin
$sock.send(str, 0, "127.0.0.1", 9999)
rescue Exception => e
p "Oops: #{e}"
end
end

$sock = UDPSocket.new

mysend("hello")
-------------------------------------------------------------------
def Send str

Aside: method names usually don't start with uppercase letters, and you
might be better to avoid them.

Incidetnally, some care also needs to be taken with the method name 'send',
because for most objects it's used to for method dispatch:

irb(main):007:0> 3.send('+',5)
=> 8

Regards,

Brian.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top