madhu.hanigadde has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, How can I create pool of sockets with some number limit. And how can I share them in some other process. Help me out with some examples.

Replies are listed 'Best First'.
Re: Socket pool
by zentara (Cardinal) on Feb 17, 2009 at 13:00 UTC
    You might find this useful Simple threaded chat server. You could easily add code to check the scalar value of the array holding the active sockets, and refuse connections if over a certain value. But you can also set that limit in the IO::Socket::INET listen option..... so I can't fully understand your question.

    You can share them with other applications thru the filenos of the filehandles and pid of the server script. Look in /proc/$pid/fd for a list of your sockets. As long as you have user rights to those processes, you can read/write to them.


    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
Re: Socket pool
by Illuminatus (Curate) on Feb 17, 2009 at 12:26 UTC
    As has been previously posted, you can indeed create a pool of sockets with some number limit. However, I don't know how this would be useful. The whole point of a socket is that it is either connected to something, or is waiting for connections from something. I suppose you could have a bunch of sockets all connected to the same address/port, and pass them around, but unless your layer7 stuff is stateless, this would be difficult to use. As for 'sharing' sockets between processes, it kind of defeats the purpose. sockets are one of the prime methods of "inter-process communication". In *nix, each socket has an entry in the process file table. You can't move these from process to process. You can keep them via a fork, but then you are back to state issues.

    Maybe a little more context, and/or a code sample, would help?

Re: Socket pool
by puudeli (Pilgrim) on Feb 17, 2009 at 10:48 UTC

    Yes you can. Write a function that takes the limit as a parameter and creates an array of hash refs. Each hashref would contain a socket and information whether it is in use or not. Then create functions for getting and freeing a socket from the pool.

    For interprocess communication see perlipc.

    --
    seek $her, $from, $everywhere if exists $true{love};
Re: Socket pool
by zwon (Abbot) on Feb 17, 2009 at 12:30 UTC