UDP Asynchrone connection problems

Hi all !
I have made a little server-client system based on UDP protocole. the
goal
is to communicate in an asynchronous way, if some packets are lost it’s
not a problem it’s why i use UDP.
The packet must be routable so I use the same port to send and receive
packet into the server and the client (same socket).

It works but I have a problem, when a client diconnected from the server
and the server try to send a packet to the client, a ICMP message error
is
receive by the server :
“WSAECONNRESET : The virtual circuit was reset by the remote side
executing a hard or abortive close. For UPD sockets, the remote host was
unable to deliver a previously sent UDP datagram and responded with a
"Port Unreachable” ICMP packet. The application should close the socket
as
it is no longer usable."

Then the server raise an error :
server.rb:12:in `recvfrom’: An existing connection was forcibly closed by
the remote host. - recvfrom(2) (Errno::ECONNRESET)

This error breaks my socket and I can’t anymore receive or send packet,
so
how can I ignore this error ?

Here is a very simple code for testing (it works), usually I have a
timeout system when a client don’t send any packets from a certain time
but I don’t include it. The client send data with a frequency of 2 Herz
and the server with a frequency of 1Herz, usually the frequency is higher
like 60Hz (for doom-like game or others).

Try to run the server and a client then diconnect the client and wathc
what there appears on the server.

server.rb :

···

#!/usr/bin/ruby
#server

require ‘socket’

s = UDPSocket::new
s.bind(’’, 54321)

connected_client = Array::new

while true
packet = s.recvfrom(255)

if packet[0] == 'init’
new_num_client = rand(1000)
s.send(new_num_client.to_s, 0, packet[1][3], packet[1][1])
connected_client.push(new_num_client)

  Thread::new(s, packet[0], packet[1][3], packet[1][1]){|s, data, ip,

port>
puts "A new client connected"
while true
sleep(1.0)
s.send(“server data”, 0, ip, port)
end
}
else
data = packet[0].split(’|’)
puts "according to data #{data[1]} from client #{data[1]} change
some state of the server"
end
end


client.rb :


#!/usr/bin/ruby
#client

require ‘socket’

s = UDPSocket::new
s.connect(‘localhost’, 54321)
s.send(‘init’, 0)
packet = s.recvfrom(255)
my_num = packet[0].to_i

Thread::new{
while true
s.send("#{my_num}|client data : #{$*[0]}", 0)
sleep(0.5)
end
}

while true
packet = s.recvfrom(255)
puts "I received server data : #{packet[0]}"
end


Thanks by advance (I dont if it can be say).

Greg Burri

"WSAECONNRESET

You don't say what is your sytem : I suppose Windows, no ?

but which windows ?

   Microsoft Support

Guy Decoux

Sorry, the problem appears under windows (XP + ruby 1.8) and linux (debian

  • ruby1.7). (the reception of this ICMP packet is normal)
···

-----Original Message-----
From: ts decoux@moulon.inra.fr
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Date: Wed, 19 May 2004 23:23:14 +0900
Subject: Re: UDP Asynchrone connection problems

"WSAECONNRESET

You don’t say what is your sytem : I suppose Windows, no ?

but which windows ?

Microsoft Support

Guy Decoux

Hi,

I think you are doing a connect with the UDP socket that ties the two
ends together. What you are seeing is proper IP behavior. When a UDP
packet is sent to a port on which nothing is listening, the receiver
will send an ICMP port-unreachable packet to the sender. Since you are
using a connected UDP socket, the socket catches that ICMP packet and
the subsequent recv raises an exception. An unconnected socket will
ignore the ICMP packet.

You have a two options:

  1. do not issue ‘connect’ with the UDP socket. ICMP replies will be
    ignored and you can also use one socket to communicate with multiple
    hosts and ports. You will want to use a non-blocking mechanism like
    Kernel::select with a timeout, or you can be waiting forever for a
    reply that may never come.

  2. Catch the exception (since it is normal, you can use it to detect if
    your expected peer has dropped off). In this case you may also want to
    use Kernel::select since the other side may not see your packet or you
    may not see theirs (in the case that the peer is there but the packet
    dropped).

Dan

···

On May 19, 2004, at 10:30, greg.burri@net2000.ch wrote:

-----Original Message-----
From: ts decoux@moulon.inra.fr
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Date: Wed, 19 May 2004 23:23:14 +0900
Subject: Re: UDP Asynchrone connection problems

"WSAECONNRESET

You don’t say what is your sytem : I suppose Windows, no ?

but which windows ?

Microsoft Support

Guy Decoux

Sorry, the problem appears under windows (XP + ruby 1.8) and linux
(debian

  • ruby1.7). (the reception of this ICMP packet is normal)