Writing a (multithreaded) socket server in PHP
|Transfering data...||Apr 4, 2010|
|Finding the high...||Jan 8, 2010|
|Selecting on pre...||Nov 23, 2009|
|Forcing foreach ...||Oct 11, 2009|
|Creating an anym...||Oct 7, 2009|
|Bitmask access l...||Aug 5, 2009|
|Recursive templa...||Jul 27, 2009|
|Writing a (multi...||Jul 15, 2009|
|Cloning external...||Jun 23, 2009|
|Performing AJAX ...||May 9, 2009|
|Extendable datal...||Apr 9, 2009|
|Adding javascrip...||Nov 8, 2008|
|Custom session h...||Aug 2, 2008|
|Writing maintain...||May 8, 2008|
|Using PHP's Soap...||May 6, 2008|
|Dear FragFrog, You..||Jan 12, 2013|
|I've taken a look ..||Mar 30, 2012|
|Another interestin..||Feb 17, 2012|
|I am also using Ap..||Jul 21, 2011|
|Yeah extension_dir..||Jul 21, 2011|
|You're sure the li..||Jul 20, 2011|
|I've downloaded se..||Jul 20, 2011|
|As someone recentl..||Mar 29, 2011|
|Thats actually qui..||Jul 19, 2010|
|Peculiar, it works..||Jul 13, 2010|
|Doesn't work with ..||Jul 12, 2010|
|Don't tell anyone,..||Jul 5, 2010|
|Yeah. "Paint" does..||Jul 5, 2010|
|Woops, fixed that,..||May 19, 2010|
|It's catEgory, not..||May 18, 2010|
PHP has sockets like many other languages. Their use is diverse, from creating gameservers to chatrooms to simple proxies. You can use a Flash client to talk to your PHP server directly through sockets, bypassing a huge load of overhead created by a webserver. Additionally, using PHP sockets you can create a website that recieves updates not by constant polling, like AJAX, but by a push from the server - this technique is sometimes called COMET and is very, very awesome in many regards.
A while ago I worked on a socket gameserver in PHP, and a basic version of it can still be found here - complete with connectserv, monsterserver and authserv. Some parts are quite well finished and polished, others are barely working, let alone documented, but you should be able to get the general idea from it if you so wish. However, I have since discontinued work on that project for a simple reason: PHP is, by nature, singlethreaded.
You can run PHP in commandline, bypassing a webserver alltogether. You can even start a new PHP instance from within a PHP instance, giving you an alternative to using threads (which are unsuported on windows and only marginally suported under linux). A quick search learns us that indeed some people have tried this approach, but as he justly notes at the end of that article, it is not true multithreading. Overall, opening several PHP instances is a poor substitute, especially since you can only open a socket listener on an empty port. Inherent in this then is the fact that your clients will inevitably be waiting on eachother - not so much if you code well, but still a long or slow databurst from one client will delay the others. There are workarounds, of course, but few and laborous.
I have said, and still claim, that PHP can do pretty much anything. But a socketserver in PHP is simply using the wrong tool for the job - as an old saying goes, if all you have is a hammer, everything looks like a nail. A while ago I started work on a new socketserver, this time in JAVA, and it has been simply awesome. Multithreading is a breeze, the language feels almost designed for the purpose of creating socketservers with (which might actually even be the case) and the time you waste writing exception handlers is gained by not having to work around an inherent problem in PHP.
Long story short: yes, you can use Sockets in PHP, but no, you really shouldn't.