Forum: Ruby "Too many open files - socket(2)" when invoking Socket.new

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bfa7bded551b934e38d141f86281447f?d=identicon&s=25 Clemens Wyss (clemens)
on 2009-05-13 16:41
Given the follwing code snippet:

require 'socket'
include Socket::Constants
socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
sockaddr = Socket.pack_sockaddr_in( 1313, 'localhost' )
socket.bind( sockaddr )
socket.listen( 5 )
client, client_sockaddr = socket.accept
client.puts "Hello from script one!"
puts "The client said, '#{client.readline.chomp}'"
client.puts "Hello from script one!"
socket.close

On my WinXP development machine this script runs as expected. Running it
on a WinXpEmbedded machine throws a
  Too many open files - socket(2) (Errno::EMFILE)
exception on the line
  socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
What could cause this exception?

Any advice help appreciated
Clemens
Cb6bbc826cd7d9238a2fae344958f7ec?d=identicon&s=25 Sandor Szücs (Guest)
on 2009-05-16 23:33
(Received via mailing list)
On 13.05.2009, at 16:41, Clemens Wyss wrote:

> Given the follwing code snippet:
>
>
> require 'socket'
> include Socket::Constants
begin
>
> socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
> sockaddr = Socket.pack_sockaddr_in( 1313, 'localhost' )
> socket.bind( sockaddr )
> socket.listen( 5 )
> client, client_sockaddr = socket.accept
> client.puts "Hello from script one!"
> puts "The client said, '#{client.readline.chomp}'"
> client.puts "Hello from script one!"
ensure
>
> socket.close
end


Try to add these statements in your code and reboot your box,
then it should not happen.

> On my WinXP development machine this script runs as expected.
> Running it
> on a WinXpEmbedded machine throws a
>  Too many open files - socket(2) (Errno::EMFILE)
> exception on the line
>  socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
> What could cause this exception?


Too many open files for your OS results in such an exception.

regards, Sandor
Szücs
Bfa7bded551b934e38d141f86281447f?d=identicon&s=25 Clemens Wyss (clemens)
on 2009-05-18 07:38
Dear Sandor,
I don't see why your exception handling code should be of any help. The
exception per se is the problem.

Why (on XP embedded only) do I get
  Too many open files - socket(2) (Errno::EMFILE)
on the line
 socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
?
Also: opening the very same port with a simple VB App works, even on XP
embedded...

Still any help/advice appreciated
Clemens

Sandor Szücs wrote:
> On 13.05.2009, at 16:41, Clemens Wyss wrote:
>
>> Given the follwing code snippet:
>>
>>
>> require 'socket'
>> include Socket::Constants
> begin
>>
>> socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
>> sockaddr = Socket.pack_sockaddr_in( 1313, 'localhost' )
>> socket.bind( sockaddr )
>> socket.listen( 5 )
>> client, client_sockaddr = socket.accept
>> client.puts "Hello from script one!"
>> puts "The client said, '#{client.readline.chomp}'"
>> client.puts "Hello from script one!"
> ensure
>>
>> socket.close
> end
>
>
> Try to add these statements in your code and reboot your box,
> then it should not happen.
>
>> On my WinXP development machine this script runs as expected.
>> Running it
>> on a WinXpEmbedded machine throws a
>>  Too many open files - socket(2) (Errno::EMFILE)
>> exception on the line
>>  socket = Socket.new( AF_INET, SOCK_STREAM, 0 )
>> What could cause this exception?
>
>
> Too many open files for your OS results in such an exception.
>
> regards, Sandor
> Sz�cs
Cb6bbc826cd7d9238a2fae344958f7ec?d=identicon&s=25 Sandor Szücs (Guest)
on 2009-05-18 14:49
(Received via mailing list)
On 18.05.2009, at 07:39, Clemens Wyss wrote:

> I don't see why your exception handling code should be of any help.
> The
> exception per se is the problem.

True, but I thought if you try to run your code and it doesn't close
the sockets,
then you get open files.... It's better to ensure the close operation.

regards, Sandor
Szücs
This topic is locked and can not be replied to.