# Ruby Bug? Windows and UDPSocket and Errno:ENOTCONN

**URL:** <https://www.ruby-forum.com/t/ruby-bug-windows-and-udpsocket-and-errno-enotconn/148182>\
**Category:** Ruby\
**Created:** [September 10, 2008, 6:21am UTC](https://www.ruby-forum.com/t/ruby-bug-windows-and-udpsocket-and-errno-enotconn/148182 "2008-09-10T06:21:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [September 10, 2008, 6:21am UTC](https://www.ruby-forum.com/t/ruby-bug-windows-and-udpsocket-and-errno-enotconn/148182/1 "2008-09-10T06:21:30Z")

</div>

If I have a pair of Win2k Ruby processes bouncing UDP packets back and  
forth…

After awhile one of them dies with Errno::ENOTCONN

The code works under Linux but not under Win2k, neither on ruby-1.8.6  
nor 1.8.7.

Any guesses? Suggestions? Workarounds?

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand

======================================================================

#!/usr/bin/ruby -w  
STDOUT.sync = true

require ‘socket’

SERVER\_PORT = 7272  
CLIENT\_PORT = 7373

SERVER\_HOST = ‘localhost’  
CLIENT\_HOST = ‘localhost’

PACKET\_LENGTH = 64

SLEEP\_TIME = 3 #seconds

MESSAGE = “I say, boy, I resemble that remark!”

class AbstractDaemon

```
def initialize(to_host, send_port, recv_port)
   @send_port = send_port
   @recv_port = recv_port
   @host = to_host
   @recv_socket = UDPSocket.new
   print "Listening on port: #{@recv_port}..."
   @recv_socket.bind(0, @recv_port)
   puts 'ok'
   @send_socket = UDPSocket.new
   print "Listening on port: #{@send_port}..."
   @send_socket.connect(0, @send_port)
   puts 'ok'
   do_stuff
end

def do_stuff
   raise Exception.new("Dummy method not implemented")
end

def send
   puts "Sending message: #{MESSAGE}"
   @send_socket.syswrite(MESSAGE)
end

def receive
   packet,address = *@recv_socket.recvfrom(PACKET_LENGTH)
   puts "Received message: #{packet}"
end

def pause
   print "Sleeping"
   SLEEP_TIME.times do
      print '.'
      sleep(1)
   end
   puts ""
end

```

end

class Server \< AbstractDaemon

```
def initialize
   super(CLIENT_HOST, CLIENT_PORT, SERVER_PORT)
end

def do_stuff
   while true
      receive
      send

```

# pause

```
   end
end

```

end

class Client \< AbstractDaemon

```
def initialize
   super(SERVER_HOST, SERVER_PORT, CLIENT_PORT)
end

def do_stuff
   while true
      send
      receive

```

# pause

```
   end
end

```

end

if ARGV[0] == ‘server’  
Server.new  
elsif ARGV[0] == ‘client’  
Client.new  
else  
puts “Usage: #{$0} \<client | server\>”  
end

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [September 10, 2008, 9:11am UTC](https://www.ruby-forum.com/t/ruby-bug-windows-and-udpsocket-and-errno-enotconn/148182/2 "2008-09-10T09:11:25Z")

</div>

From: “John C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> If I have a pair of Win2k Ruby processes bouncing UDP packets back and  
> forth…
> 
> After awhile one of them dies with Errno::ENOTCONN
> 
> The code works under Linux but not under Win2k, neither on ruby-1.8.6  
> nor 1.8.7.
> 
> Any guesses? Suggestions? Workarounds?

Replaced connect/syswrite, with send:

— john\_carter\_udp\_enotconn.orig.rb 2008-09-09 23:27:04.491375000  
-0700  
+++ john\_carter\_udp\_enotconn.rb 2008-09-09 23:27:50.975750000 -0700  
@@ -28,7 +28,6 @@  
puts ‘ok’  
@send\_socket = UDPSocket.new  
print “Listening on port: #{@send\_port}…”

- 

```
  @send_socket.connect(0, @send_port)
  puts 'ok'
  do_stuff

```

end  
@@ -39,7 +38,7 @@

def send  
puts “Sending message: #{MESSAGE}”

- 

```
  @send_socket.syswrite(MESSAGE)

```

- 

```
  @send_socket.send(MESSAGE, 0, @host, @send_port)

```

end

def receive

Hope this helps,

Bill

---

<div class="post-metadata">

**Author:** ![amin1426](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@amin1426](https://www.ruby-forum.com/u/amin1426)\
**Post date:** [September 11, 2008, 2:56am UTC](https://www.ruby-forum.com/t/ruby-bug-windows-and-udpsocket-and-errno-enotconn/148182/3 "2008-09-11T02:56:28Z")

</div>

> > Any guesses? Suggestions? Workarounds?  
> > On Wed, 10 Sep 2008, Bill K. replied:  
> > Replaced connect/syswrite, with send:

I thought, hmm, I’m pretty sure thats what I tried first…

Anyhoo, I’ll try your suggestion and…

…it worked!

> Hope this helps,

So I went back to the original code and saw we had…  
UDPSocket.open.send

Ping! Light goes on! Everytime we do UDPSocket.open we open an fd and  
don’t close it.

The mystik Errno::ENOTCONN arises because the open failed (silently)  
because we had run out of file handles!

Thanks!

John C. Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : removed\_email\_address@domain.invalid  
New Zealand
