![]() |
|
Perl: the Markov chain saw | |
PerlMonks |
Re^2: Chat server impossible with Perl?by MarkusLaker (Beadle) |
on Feb 05, 2005 at 00:23 UTC ( #428258=note: print w/replies, xml ) | Need Help?? |
How do I use threads to listen for new connections whilst maintaining responsiveness to existing ones
You may not need threads to do that. Although perlipc and perlfunc don't mention it, you can run select on a generic socket, at least on Unix. In more detail: You open perlipc, find the section labelled 'Internet TCP Clients and Servers', and use the recipe to create a generic server socket: socket(), setsockopt(), bind(), listen(). You now build a bitmask and go into a select loop. (If you've not used select before, perlfunc explains how. select is a way of sleeping until there's some activity on one or more of any number of connections.) When select says there's something ready for your generic socket to read, it means there's an incoming connection attempt: so you accept the connection, flip a bit in your bitmask to represent the new connection, and go back to the top of your select loop. You're now in an interesting position: select will return when you're ready either to accept a new connection or to read data from an existing connection. So, if you can service an incoming packet quickly enough, a single fibre is all you need. That said, I'd be fascinated to know how to share globs between Perl threads. I threw away a day's work earlier this week because Perl told me it couldn't be done and (as this was work time) I decided not to spend time trying to hack it. At the very least, this restriction should be documented in perlthrtut and/or threads::shared. Markus
In Section
Seekers of Perl Wisdom
|
|